JAVA中常用的类
1.Objects类
1.0 object.equals()
改写equals方法
public class Person/*extends Object*/ { private int age; private String name; public Person() { } public Person(int age, String name) { this.age = age; this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } //Object obj=p1=new person() 多态 无法访问子类中的属性 向下转型 @Override public boolean equals(Object obj) { Person person=(Person)obj; boolean bool=this.name.equals(person.name)&&this.age==person.getAge(); return bool; } }
public class Main { public static void main(String[] args) { Person p1=new Person(18,"鹿晗"); Person p2=new Person(19,"鹿晗"); boolean bool=p1.equals(p2); System.out.println(bool); System.out.println(p1); System.out.println(p2); //重写之后的equals比较的不再是地址值 } }
打印结果:
false
Person@50cbc42f
Person@75412c2f
2.date类
import java.util.Date;
public class Main {
public static void main(String[] args) {
Date date=new Date();//无参构造 获取日期和时间
Date date1=new Date(1591093154239L);//传入一个毫秒值 获取日期和时间
System.out.println(date1);//Date重写了toString方法
System.out.println(date);
System.out.println(date.getTime());
System.out.println(System.currentTimeMillis());
}
}
打印结果:
Tue Jun 02 18:19:14 CST 2020
Tue Jun 02 18:20:12 CST 2020
1591093212547
1591093212608
3.Dateformat类
1.0 Dateformat类的format方法的使用
import java.text.SimpleDateFormat; import java.util.Date; public class Main { public static void main(String[] args) { SimpleDateFormat sdf=new SimpleDateFormat("yyyy年MM月dd日 HH时mm分ss秒"); SimpleDateFormat sdf1=new SimpleDateFormat(); Date date=new Date(); String date1=sdf.format(date); System.out.println(date1);//传参 String date2=sdf1.format(date); System.out.println(date2);//不传参 System.out.println(date); } }
打印结果:
2020年06月02日 21时17分01秒
2020/6/2 下午9:17
Tue Jun 02 21:17:01 CST 2020
2.0 Dateformat类的parse方法的使用
import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; public class Main { public static void main(String[] args) throws ParseException { SimpleDateFormat sdf=new SimpleDateFormat("yyyy年MM月dd日 HH时mm分ss秒"); Date date=sdf.parse("2020年06月02日 21时17分01秒"); System.out.println(date); } }
打印结果:
Tue Jun 02 21:17:01 CST 2020
4.Calendar类
1.0 Calendar类介绍 获取对象的方式
2.0 Calendar类的常用成员方法
public int get();
import java.util.Calendar; public class Main { public static void main(String[] args){ Calendar c=Calendar.getInstance();//多态 int year=c.get(Calendar.YEAR); System.out.println(year); int month=c.get(Calendar.MONTH)+1;//西方 0-11 东方 1-12 System.out.println(month); //int date=c.get(Calendar.DATE); int date=c.get(Calendar.DAY_OF_MONTH); System.out.println(date); } }
打印结果:
2020
6
3
public void set();
c.set(Calendar.DATE,30); int date=c.get(Calendar.DAY_OF_MONTH); System.out.println(date);
打印结果:
30
public abstract void add()
5.System类
1..0 测试程序运行的时间System.currentTimeMillis()
public class Main { public static void main(String[] args){ long begin= System.currentTimeMillis(); for(int i=1;i<=50;i++) { System.out.println(i); } long end= System.currentTimeMillis(); System.out.println("运行"+ (end-begin)+"毫秒"); } }
打印结果:
运行3毫秒
2.0 System.arraycopy()
import java.util.Arrays; public class Main { public static void main(String[] args){ int src[]={33,42,4,34,67,89}; int dest[]={45,34,35,23,53,5}; System.arraycopy(src,0,dest,0,3); System.out.println(Arrays.toString(dest)); } }
打印结果:
[33, 42, 4, 23, 53, 5]
6.StringBuild类
1.0 构造方法
public class Main { public static void main(String[] args) { StringBuilder string = new StringBuilder();//无参构造 StringBuilder string1 = new StringBuilder("abc");//有参构造 System.out.println(string); System.out.println(string1); } }
打印结果:
abc
2.0 常用方法
append方法
2.0 StringBuilder的toString方法
public class Main { public static void main(String[] args) { //String->StringBuilder StringBuilder str = new StringBuilder("helloworld");//无参构造 //StringBuilder->String String s=str.toString(); System.out.println("str:"+str); System.out.println("s:"+s); } }
打印结果:
str:helloworld
s:helloworld
7.包装类
1.0 装箱和拆箱
public class Main { public static void main(String[] args) { Integer in1=new Integer(123); Integer in2=new Integer("345"); System.out.println(in1); System.out.println(in2); int in3=Integer.valueOf(in1); int in4=Integer.valueOf(in2); System.out.println(in3); System.out.println(in4); } }
打印结果:
123
345
123
345
2.0 自动装箱和自动拆箱
3.0 基本类型与字符串类型之间\
基本类型转化为字符串
public class Main { public static void main(String[] args) { /*第一种*/ int in1=13; String s1=in1+"45"; System.out.println(s1); /*第二种*/ int in2=8976; String s2 =Integer.toString(in2); System.out.println(s2); /*第三种*/ String s3=String.valueOf(in2); System.out.println(s3); } }
打印结果:
1345
8976
8976
字符串转化为基本类型
public class Main { public static void main(String[] args) { int i=Integer.parseInt("8943294"); System.out.println(i);//8943294 double i1=Double.parseDouble("45.32"); System.out.println(i1);//45.32 } }