新的时间类(LocalDate、LocalDateTime)
java8 之前我们处理时间 大多会涉及到这几个类Date、SimpleDateFormat、Calendar ,这种处理方式复杂、存在线程隐患、国际化困难、日期加减等处理麻烦等等。 现在有了 LocalDate、LocalDateTime、DateTimeFormatter 生活就变得简单了
格式化及区域定义
设置格式化模板
private static final DateTimeFormatter DATE_TIME_FORMATTER
= DateTimeFormatter
.ofPattern("yyyy-MM-dd HH:mm:ss.SSSS");
**设置日期时区常量
**
```java
public static final ZoneId CHINA_ZONE_ID
= ZoneId
.systemDefault();
Date格式化为DateTime
@Test
public void dateToDateTime(){
Date date
= new Date();
LocalDateTime dateTime
= date
.toInstant().atZone(CHINA_ZONE_ID
).toLocalDateTime();
System
.out
.println(dateTime
);
}
LocalDate/LocalDateTime转Date
@Test
public void toDate(){
LocalDate localDate
= LocalDate
.now();
Date d1
= Date
.from(localDate
.atStartOfDay(CHINA_ZONE_ID
).toInstant());
System
.out
.println(d1
);
LocalDateTime localDateTime
= LocalDateTime
.now();
Date d2
= Date
.from(localDateTime
.atZone(CHINA_ZONE_ID
).toInstant());
System
.out
.println(d2
);
}
日期格式化
@Test
public void formatDate(){
System
.out
.println(LocalDateTime
.now().format(DATE_TIME_FORMATTER
));
}
日期加减
@Test
public void plusDay(){
LocalDateTime dateTime
= LocalDateTime
.now(CHINA_ZONE_ID
);
dateTime
=dateTime
.plusDays(1);
dateTime
=dateTime
.plusHours(-1);
dateTime
=dateTime
.plusMinutes(30);
System
.out
.println(dateTime
.format(DATE_TIME_FORMATTER
));
}
日期时间间隔
@Test
public void betweenDay(){
LocalDateTime startDate
= LocalDateTime
.of(2019,07,01,12,12,22);
LocalDateTime endDate
= LocalDateTime
.of(2019,07,03,12,12,22);
Long withSecond
= endDate
.atZone(CHINA_ZONE_ID
).toEpochSecond() - startDate
.atZone(CHINA_ZONE_ID
).toEpochSecond();
System
.out
.println(withSecond
/60/60/24);
LocalDate startDate2
= LocalDate
.of(2019,07,01);
LocalDate endDate2
= LocalDate
.of(2019,07,03);
Long withSecond2
= endDate2
.toEpochDay() - startDate2
.toEpochDay();
System
.out
.println(withSecond2
);
}
第一天and最后一天
@Test
public void theLastDay(){
LocalDateTime dateTime
= LocalDateTime
.of(2019,07,03,12,12,22);
dateTime
= dateTime
.with(TemporalAdjusters
.firstDayOfMonth());
System
.out
.println(dateTime
);
dateTime
= dateTime
.with(TemporalAdjusters
.lastDayOfMonth());
System
.out
.println(dateTime
);
dateTime
= LocalDateTime
.now();
int dayOfMonth
= dateTime
.getDayOfMonth();
System
.out
.println(dayOfMonth
);
int dayOfWeek
= dateTime
.getDayOfWeek().getValue();
System
.out
.println(dayOfWeek
);
}