Java——Date
一、概念
java.util.Date 表示特定的瞬间,精确到毫秒。
二、Date类的构造方法
2.1、public Date()
创建的对象,表示的是当前计算机系统时间
public class DateTest {
public static void main(String[] args) throws ParseException {
Date date = new Date();
System.out.println(date); // Fri May 14 15:29:33 CST 2021
}
}
2.2、public Date(long t)
可以指定毫秒创建Date对象,毫秒是从历元(1970-01-01 00:00:00)开始计算的时间。从这一刻开始计作0毫秒。
public class DateTest {
public static void main(String[] args) throws ParseException {
long num = 1620977560284L;
Date date = new Date(num);
System.out.println(date); // Fri May 14 15:32:40 CST 2021
}
}
三、getTime
public long getTime()
getTime方法用来获取日期对象的毫秒值
public class DateTest {
public static void main(String[] args) throws ParseException {
Date date = new Date();
System.out.println(date.getTime()); // 1620977963498
}
}
四、setTime
public void setTime(long t)
setTime方法用来给日期对象设置毫秒值
public class DateTest {
public static void main(String[] args) throws ParseException {
Date date = new Date();
date.setTime(1620977963498L);
System.out.println(date); // Fri May 14 15:39:23 CST 2021
}
}

浙公网安备 33010602011771号