1.Object类里的toString()
public String toString() {
return getClass().getName() + "@" + Integer.toHexString(hashCode());
}
但是一般我们自己写的自定义类,要重写toString()
2.Object类里的equals()
public boolean equals(Object obj) {
return (this == obj);
}
由于Object类里的equals()方法比较的是两个对象地址值,所以没啥意义
所以我们自己写的自定义类,要重写equals方法,让它变得有意义
class Person {
private String name;
private int age;
public Person() {
super();
}
public Person(String name, int age) {
super();
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String toString() {
return name + "..." + age;
}
public boolean equals(Object obj) {
if(obj == this) {
return true;
}
if(!(obj instanceof Person)) {
return false;
}
Person p = (Person)obj;
return this.name.equals(p.name) && this.age == p.age;
}
}
3.System类
System.currentTimeMillis():获取当前系统的毫秒值
System.exit(0):退出JVM
System.arraycopy(src, srcPos, dest, destPos, length):复制数组
4.Date类
Date()
Date(long l)
setTime(long l)
getTime()
5.日期对象的格式化和解析
格式化:
Date date = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss E");
String time = sdf.format(date);
System.out.println(time);
解析:
String time = "2000-10-10";
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date date = sdf.parse(time);
System.out.println(date);
6.你来到这个世界有多少天
class Demo {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入您的出生日期(2000-10-10):");
String start = sc.nextLine();
System.out.println("请输入您现在的日期(2018-04-24):");
String now = sc.nextLine();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date dateStart = sdf.parse(start);
Date dateNow = sdf.parse(now);
int dayNum = (dateNow.getTime() - dateStart.getTime()) / 1000 / 60 / 60 / 24;
System.out.println(dayNum);
}
}
7.Calendar类
Calendar由于是抽象类
获取对象的方法:
Calendar c = Calendar.getInstance();
c.set(参数);
c.add(参数);
c.get(参数);
8.Integer
是int类型的包装类
具有自动装箱拆箱的新特性
装箱:Integer in = new Integer(1);
拆箱:Integer in new Integer(10);
int i = in.intValue();
Integer integer = 10;
9.String的切割功能
String s = "C:\\aaa\\bbb\\c.txt";
String[] strs = s.split("\\\\");
System.out.println(Arrays.toString(strs));
|
|