LocalDate、LocalTime、LocalDateTime、Instant

首先这些类是线程安全的,对于它的任何操作都会产生一个新的实例,这和 String 类是一样的。其次它不存储或表示时区。 相反,它是对日期时间的描述。

代码 打印 说明
LocalDateTime.now(); 2022-03-16T15:41:28.772 日期时间
LocalDate.now(); 2022-03-16 日期
LocalTime.now(); 15:41:28.773 时间
LocalDateTime.of(2022, 3, 16, 15, 5, 0) 2022-03-16T15:05 指定时间
Instant.now().toEpochMilli() 1647691710153 时间戳毫秒
LocalDateTime time1 = LocalDateTime.now();
LocalDateTime time2 = LocalDateTime.now();

time1.isAfter(time2); time1.isBefore(time2);//比较时间
time1.plusDays(1L); time1.minusHours(1L);//加减时间日期
LocalDateTime.parse("2021-11-19T15:16:17");//解析时间
LocalDateTime.of(2019, 11, 30, 15, 16, 17);//指定日期时间
LocalDateTime.now(ZoneId.of("Asia/Jakarta"));//其他时区相对此服务器时区的时间

LocalDateTime localDateTime = LocalDateTime.now();
//LocalDateTime转Instant
Instant localDateTime2Instant = localDateTime.atZone(ZoneId.systemDefault()).toInstant();
System.out.println("LocalDateTime 毫秒数:"+localDateTime2Instant.toEpochMilli());

OffsetDateTime 、ZonedDateTime

代码 打印 说明
OffsetDateTime.now(); 2022-03-16T15:59:35.591+08:00 带有时区偏移量的日期时间
ZonedDateTime.now(); 2022-03-16T15:59:35.591+08:00[Asia/Shanghai] 真正带有完整时区信息的日期时间类

Duration、Period

  • Duraction:时间的区间,用来度量秒和纳秒之间的时间值
  • Period:一段时间的区间,用来度量年月日和几天之间的时间值

Duraction:

LocalTime currentLocalTime = LocalTime.now();
LocalTime stopLocalTime = LocalTime.of(2022, 3, 16);
Duration duration = Duration.between(currentLocalTime, stopLocalTime);
System.out.println(duration);
System.out.println(duration.isZero());            //Duration区间是否为0
System.out.println(duration.isNegative());        //Duration区间是否为负

System.out.println(duration.getSeconds());        //Duration区间值的秒数
System.out.println(duration.getNano());            //Duration区间值的纳秒数
System.out.println(duration.getUnits());        //Duration的度量单位

System.out.println(duration.toDays());            //Duration区间相差几天
System.out.println(duration.toHours());            //Duration区间差几小时
System.out.println(duration.toMinutes());        //Duration区间相差几分钟
System.out.println(duration.toMillis());        //Duration区间相差几毫秒

Period:

LocalDate localDate = LocalDate.now();
LocalDate localDate2 = LocalDate.of(2022, 3, 16);
Period period = Period.between(localDate, localDate2);
System.out.println(period);
System.out.println(period.isZero());            //区间是否为0        
System.out.println(period.isNegative());        //区间是否为为负

System.out.println(period.getYears());            //区间的相差几年
System.out.println(period.getMonths());            //区间的相差几月
System.out.println(period.getDays());            //区间的相差几日

System.out.println(period.toTotalMonths());        //区间相差多少个月

TemporalAdjuster 、TemporalAdjusters

TemporalAdjuster 是函数接口,仅有一个带Temporal对象参数的抽象方法adjustInto()。

TemporalAdjusters类有很多预定义的static方法返回TemporalAdjuster对象。

//当前月的第一天
LocalDate.now().with(TemporalAdjusters.firstDayOfMonth());
//当前月的最后一天
LocalDate.now().with(TemporalAdjusters.lastDayOfMonth());
//当年的第一天
LocalDate.now().with(TemporalAdjusters.firstDayOfYear());

更多参考:https://docs.oracle.com/javase/8/docs/api/java/time/temporal/TemporalAdjusters.html

LocalDate localDate = LocalDate.of(2022, 3, 16);
TemporalAdjuster temporalAdjuster = t -> t.plus(Period.ofDays(14));
LocalDate result = localDate.with(temporalAdjuster);

格式化日期

DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
LocalDateTime.now().format(formatter);

LocalDateTime dateTime = LocalDateTime.parse("2021-01-02 10:00:00", formatter);

Date 和 LocalDateTime 互转

//Date 转 LocalDateTime
LocalDateTime.ofInstant(new Date().toInstant(), ZoneId.systemDefault());
// LocalDateTime 转 Date
Date.from(LocalDateTime.now().atZone(ZoneId.systemDefault()).toInstant());

Timestamp和LocalDateTime 互转

//Timestamp 转 LocalDateTime 
Timestamp time = Timestamp.from(Instant.now());
LocalDateTime localDateTime = time.toLocalDateTime();
//LocalDateTime  转 Timestamp
Timestamp time = Timestamp.valueOf(LocalDateTime.now());

//timestamp 转 LocalDateTime 
Instant instant = Instant.ofEpochMilli(timestamp);
LocalDateTime.ofInstant(instant, ZoneId.systemDefault());

Instant 和 Timestamp 互转

//Instant 转 Timestamp  
Timestamp.from(Instant.ofEpochSecond(1647705600L));
//Timestamp 转 Instant
Timestamp.toInstant();

Long 转 Timestamp

Timestamp.from(Instant.ofEpochSecond(startTime)

1.LocalDate转Date

LocalDate nowLocalDate = LocalDate.now();

Date date = Date.from(localDate.atStartOfDay(ZoneOffset.ofHours(8)).toInstant());

2.LocalDateTime转Date

LocalDateTime localDateTime = LocalDateTime.now();

Date date = Date.from(localDateTime.atZone(ZoneOffset.ofHours(8)).toInstant());

3.Date转LocalDateTime(LocalDate)

Date date = new Date();

LocalDateTime localDateTime = date.toInstant().atZone(ZoneOffset.ofHours(8)).toLocalDateTime();

LocalDate localDate = date.toInstant().atZone(ZoneOffset.ofHours(8)).toLocalDate();

4.LocalDate转时间戳

LocalDate localDate = LocalDate.now();

long timestamp = localDate.atStartOfDay(ZoneOffset.ofHours(8)).toInstant().toEpochMilli();

5.LocalDateTime转时间戳

LocalDateTime localDateTime = LocalDateTime.now();

long timestamp = localDateTime.toInstant(ZoneOffset.ofHours(8)).toEpochMilli();

6.时间戳转LocalDateTime(LocalDate)

long timestamp = System.currentTimeMillis();

LocalDate localDate = Instant.ofEpochMilli(timestamp).atZone(ZoneOffset.ofHours(8)).toLocalDate();

LocalDateTime localDateTime = Instant.ofEpochMilli(timestamp).atZone(ZoneOffset.ofHours(8)).toL


注意:本文归作者所有,未经作者允许,不得转载