获取当前时间(Date类型)
import java.util.Date;
public static void main(String[] args) {
Date nowDate = new Date();
System.out.println(nowDate);
}
输出:Mon Apr 12 16:09:29 CST 2021
获取当前时间(Instant类型)
import java.time.Instant;
import java.util.concurrent.TimeUnit;
public static void main(String[] args) {
//东八区当前时间
Instant nowDate = Instant.now().plusMillis(TimeUnit.HOURS.toMillis(8));
System.out.println(nowDate);
}
输出:2021-04-12T16:33:18.334Z
获取当前时间毫秒数(System获取、Date获取、Calendar获取、Instant获取)
import java.time.Instant;
import java.util.Calendar;
import java.util.Date;
import java.util.concurrent.TimeUnit;
public static void main(String[] args) {
//方法一
System.out.println(System.currentTimeMillis());
//方法二
Date date = new Date();
System.out.println(date.getTime());
//方法三
Calendar calendar = Calendar.getInstance();
long time = calendar.getTimeInMillis();
System.out.println(time);
//方法四
Instant now = Instant.now().plusMillis(TimeUnit.HOURS.toMillis(8));
System.out.println(now.toEpochMilli());
}
输出:1618217202418 1618217202419 1618217202425 1618246002438