Java 8 之前使用的时间类:Date存在缺点:
Not thread safe - java.util.Date是非线程安全的,使用时必须处理并发问题。而新的时间类是不可变的,并且没有 set 方法。
Poor design - java.util.Date中日期从1900开始,月从1开始,天从0开始,十分不统一;原来的API提供了很少的时间操作。
Difficult time zone handing - 原来的API需要花费大量代码处理区域时间。
Java 8 加入了新的API:java.time
Java.time包中的是类是不可变且线程安全的。新的时间及日期API位于java.time中
- Local - 简单时间API
- Zoned - 区域时间API
* Instance 时间戳
* LocalDate 不包含时间的日期
* LocalTime 不包含日期的时间
* LocalDateTime 包含时间及日期(没有偏移信息/时区)
* ZonedDateTime 包含时区的完整的日期时间,偏移量是以UTC/格林威治时间为基准的。
Local Date-Time 的简单使用
public String Local() {
//获取当前日期时间
LocalDateTime currentTime = LocalDateTime.now();
System.out.println("当前时间:"+currentTime);
LocalDate date1 = currentTime.toLocalDate();
System.out.println("date1:"+ date1);
Month month = currentTime.getMonth();
int day = currentTime.getDayOfMonth();
int seconds = currentTime.getSecond();
System.out.println("月: " + month +", 日: " + day +", 秒: " + seconds);
//给定了日期与年份 date2:2012-12-10T16:10:38.992224100
LocalDateTime date2 = currentTime.withDayOfMonth(10).withYear(2012);
System.out.println("date2:"+ date2);
//给定日期 date3:2014-12-12
LocalDate date3 = LocalDate.of(2014, Month.DECEMBER,12);
System.out.println("date3:"+ date3);
//给定时间 date4:22:15
LocalTime date4 = LocalTime.of(22,15);
System.out.println("date4:" + date4);
//解析字符串 date5:20:15:30
LocalTime date5 = LocalTime.parse("20:15:30");
System.out.println("date5:" + date5);
return "1";
}
运行结果
当前时间:2019-12-18T16:10:38.992224100
date1:2019-12-18
月: DECEMBER, 日: 18, 秒: 38
date2:2012-12-10T16:10:38.992224100
date3:2014-12-12
date4:22:15
date5:20:15:30
Zoned 的简单使用
public String Zoned() {
//获取当前时间日期
//date1:2012-12-12T13:05:10+08:00[Asia/Shanghai]
ZonedDateTime date1 = ZonedDateTime.parse("2012-12-12T10:10:10+05:05[Asia/Shanghai]");
System.out.println("date1:"+date1);
//ZoneId:Europe/Paris ???
ZoneId id = ZoneId.of("Europe/Paris");
System.out.println("ZoneId:"+ id);
//Asia/Shanghai
ZoneId currentZone = ZoneId.systemDefault();
System.out.println(currentZone);
return "2";
}
运行结果:
date1:2012-12-12T13:05:10+08:00[Asia/Shanghai]
ZoneId:Europe/Paris
Asia/Shanghai
Chrono Units Enum
java.time.temporal.ChronoUnit
枚举类用来用表示年、月、日等,这取代原来使用 int值来表示
public String ChronoUnit() {
//Get the current date
LocalDate today = LocalDate.now();
System.out.println("Current date: " + today);
//add 1 week to the current date
LocalDate nextWeek = today.plus(1, ChronoUnit.WEEKS);
System.out.println("Next week: " + nextWeek);
//add 1 month to the current date
LocalDate nextMonth = today.plus(1, ChronoUnit.MONTHS);
System.out.println("Next month: " + nextMonth);
//add 1 year to the current date
LocalDate nextYear = today.plus(1, ChronoUnit.YEARS);
System.out.println("Next year: " + nextYear);
//add 10 years to the current date
LocalDate nextDecade = today.plus(1, ChronoUnit.DECADES);
System.out.println("Date after ten year: " + nextDecade);
return null;
}
运行结果:
Current date: 2019-12-18
Next week: 2019-12-25
Next month: 2020-01-18
Next year: 2020-12-18
Date after ten year: 2029-12-18
Period and Duration
Java 8 中两个类Period和Duration处理时间差:
- Period - 处理日期差。
- Duration - 处理时间差。
public String pd() {
/*****testPeriod*****/
//Get the current date
LocalDate date1 = LocalDate.now();
System.out.println("Current date: " + date1);
//add 1 month to the current date
LocalDate date2 = date1.plus(1, ChronoUnit.MONTHS);
System.out.println("Next month: " + date2);
Period period = Period.between(date2, date1);
System.out.println("Period: " + period);
/*****testDuration*****/
LocalTime time1 = LocalTime.now();
Duration twoHours = Duration.ofHours(2);
LocalTime time2 = time1.plus(twoHours);
Duration duration = Duration.between(time1, time2);
System.out.println("Duration: " + duration);
return null;
}
运行结果:
Current date: 2019-12-18
Next month: 2020-01-18
Period: P-1M
Duration: PT2H
Backward Compatibility
toInstant()方法用来将老的Date对象转换为LocalDateTime或ZonedDateTime对象
public String testBackwardCompatability() {
//Get the current date
Date currentDate = new Date();
System.out.println("Current date: " + currentDate);
//Get the instant of current date in terms of milliseconds
Instant now = currentDate.toInstant();
ZoneId currentZone = ZoneId.systemDefault();
LocalDateTime localDateTime = LocalDateTime.ofInstant(now, currentZone);
System.out.println("Local date: " + localDateTime);
ZonedDateTime zonedDateTime = ZonedDateTime.ofInstant(now, currentZone);
System.out.println("Zoned date: " + zonedDateTime);
return null;
}
运行结果:
Current date: Wed Dec 18 16:38:04 CST 2019
Local date: 2019-12-18T16:38:04.741
Zoned date: 2019-12-18T16:38:04.741+08:00[Asia/Shanghai]
参考文档:https://blog.csdn.net/weixin_37547589/article/details/90064465
来源:CSDN
作者:LuckyGuyy
链接:https://blog.csdn.net/qq_36821220/article/details/103598823