来自java.util.Date
日期
封装一个毫秒值,表示一个具体的时间
创建对象
new Date();
封装系统当前时间毫秒值
new Date(9000000000000L);
方法
getTime()取出毫秒值
setTime(long t)设定毫秒值
compareTo(Date d)比较大小
public int compareTo(Date anotherDate) {
long thisTime = getMillisOf(this);
long anotherTime = getMillisOf(anotherDate);
return (thisTime<anotherTime ? -1 : (thisTime==anotherTime ? 0 : 1));
}
a.compareTo(b)
a>b 则返回1
a=b 则返回0
a<b 则返回-1
例子
import java.text.SimpleDateFormat;
import java.util.Date;
public class Test1Date {
public static void main(String[] args) {
Date a = new Date();
Date b = new Date(9000000000000L);
System.out.println(a);
System.out.println(b);
System.out.println(a.getTime());
System.out.println(b.getTime());
a.setTime(0L);
System.out.println(a);
System.out.println("==============");
System.out.println(a.compareTo(b));
System.out.println(b.compareTo(a));
System.out.println(a.compareTo(a));
System.out.println(b.compareTo(b));
System.out.println("==========");
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String s1 = sdf.format(a);
String s2 = sdf.format(b);
System.out.println(s1);
System.out.println(s2);
}
}
运行结果
Fri Jul 20 00:05:37 CST 2018
Thu Mar 15 00:00:00 CST 2255
1532016337755
9000000000000
Thu Jan 01 08:00:00 CST 1970
==============
-1
1
0
0
==========
1970-01-01 08:00:00
2255-03-15 00:00:00