在Java中,Date类和Calendar 在多线程中使用会有线程不安全问题
这里给一个 LocalDateTime
使用案例
- jdk8:
public static void main(String[] args) {
System.out.println((new Date()).getTime() / 1000);
System.out.println(LocalDate.now());
System.out.println(LocalDateTime.now());//当前时区时间
System.out.println(Instant.now());//很明显的是1时区时间
System.out.println(LocalDateTime.of(LocalDate.now(), LocalTime.MIN));
System.out.println(LocalDateTime.of(LocalDate.now(), LocalTime.MAX));
LocalDateTime of = LocalDateTime.of(LocalDate.now(), LocalTime.MIN);//当天0点时间
long l = of.toInstant(ZoneOffset.ofHours(8)).toEpochMilli();//转为时间戳
System.out.println(l);
of = LocalDateTime.of(LocalDate.now(), LocalTime.MAX);//当天24:59时间
l = of.toInstant(ZoneOffset.ofHours(8)).toEpochMilli();//转为时间戳
System.out.println(l);
of = LocalDateTime.of(LocalDate.now().plusDays(-0), LocalTime.MIN);//前n天时间,0点
System.out.println(of);
}
结果:
1607862555
2020-12-13
2020-12-13T20:29:15.739
2020-12-13T12:29:15.740Z
2020-12-13T00:00
2020-12-13T23:59:59.999999999
1607788800000
1607875199999
2020-12-13T00:00