java中的Date类
JDK 8 中,Date类只有两个构造方法是可以使用的
- Date(); 用来创建当前日期时间的Date对象
- Date(long date); 用于创建指定时间的Date对象,其中date参数表示1970年1月1日0时0分0秒(称为历元)以来的毫秒数,即时间戳。1s = 1000ms,与当前所在时区有关,亚洲大概8点。
- Date(int year,int month,int date); 指定创建的日期对象的年月日
- Date(int year,int month,int date,int hrs,int min); 指定创建的日期对象的年月日时分
成员方法:
-
public String toLocalString();
返回本地的格式日期信息
-
public boolean after(Date when);
测试此日期是否在指定日期之后,
返回:当此对象表示的时间比when表示的时间晚,才返回ture,否则返回false
-
public boolean before(Date when);
测试此日期是否在指定日期之前
返回:当此对象表示的时间比when表示的早,返回ture,否则返回false
-
public int compareTo(Date anotherDate);
比较两个日期的顺序
返回:当此对象表示的时间与anotherDate的时间相等,则返回0
此对象的时间在anotherDate之前则返回小于0的值
否则返回大于0的值
-
public void setTime(long time);
设置此Date对象的时间,以表示1970连1月1日00:00:00 GET 以后time毫秒的时间点
与当前所在时区有关
例:time = 1000 ;
则表示的是 1970连1月1日00:00:01 GET
import java.util.Date;
public class Item{
public static void main(String[] args){
Date now = new Date();
Date time = new Date();
System.out.println(now.toLocalString());
System.out.println(time);
}
}