赞
踩
JDK8之前的System.currentTimeMillis()、Date、Calender日期,它们面临的问题是:
Java8吸收了Joda-Time的精华,以一个新的开始为Java创建优秀的API。新的java.time中包含了所有关于LocalDate、LocalTime、LocalDateTime、ZonedDateTime和Duration类。
Date类新增了toInstant()方法,用于把Date转换成新的表示形式。
新日期类和Date的区别?
LocalDate、LocalTime、LocalDateTime类它们的实例是不可变的对象(final修饰类),分别表示使用ISO-8601日历系统的日期、时间、日期和时间。它们提供了简单的本地日期或时间,并不包含当前时间信息,也不包含与时区相关的信息。
ps:ISO-8601日历系统是国际标准化组织制定的现代公民的日期和时间的表示法,也就是公历。
//Date转Instant
Instant instant = new Date().toInstant();
//Date转LocalDate
Instant instant = new Date().toInstant();
LocalDate localDate = instant.atZone(ZoneId.systemDefault()).toLocalDate();
//Date转LocalDateTime
Instant instant = new Date().toInstant();
LocalDateTime localDateTime = instant.atZone(ZoneId.systemDefault()).toLocalDateTime();
LocalDateTime localDateTime = LocalDateTime.ofInstant(instant, ZoneId.systemDefault());
//Instant转Date
Date date= Date.from(Instant.now());
//LocalDate转Date
Instant instant = LocalDate.now().atStartOfDay(ZoneId.systemDefault()).toInstant();
Date date= Date.from(instant);
//LocalDateTime转Date
Instant instant = LocalDateTime.now().toInstant(ZoneOffset.of("+8"));
Instant instant = LocalDateTime.now().atZone(ZoneId.systemDefault()).toInstant();
Date date = Date.from(instant);
java.time.format.DateTimeFormmatter
类提供了三种格式化方法:
常用方法:
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
String localDateStr = formatter.format(LocalDate.now());
String localDateTimeStr = formatter.format(LocalDateTime.now());
//Instant不能直接使用DateTimeFormatter格式化,需要先转换成LocalDate和LocalDateTime
String instantStr = formatter.format(Instant.now().atZone(ZoneId.systemDefault()).toLocalDate());
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
//LocalDate不能使用该格式会抛异常,因为没有时分秒
String localDateTimeStr = formatter.format(LocalDateTime.now());
//Instant
String instant = formatter.format(Instant.now().atZone(ZoneId.systemDefault()).toLocalDateTime());
@Test
public void test() {
LocalDate bgnDate = LocalDate.of(2020, 10, 10);
LocalDate endDate = LocalDate.of(2022, 5, 5);
Period period = Period.between(bgnDate, endDate);
int years = period.getYears();
int months = period.getMonths();
int days = period.getDays();
long totalMonths = period.toTotalMonths();
System.out.println("总月份:"+totalMonths);
System.out.println("相差年份:"+years);
System.out.println("相差月份:"+months);
System.out.println("相差天数:"+days);
}
运行结果:
只有满足一年或一个月才算
ps:period
的between
方法实现也是使用LocalDate的untils
方法
@Test
public void test() {
LocalDate bgnDate = LocalDate.of(2020, 10, 10);
LocalDate endDate = LocalDate.of(2022, 5, 5);
Duration duration = Duration.between(bgnDate, endDate);
long toDays = duration.toDays();
long years = duration.get(ChronoUnit.YEARS);
long months = duration.get(ChronoUnit.MONTHS);
long days = duration.get(ChronoUnit.DAYS);
long weeks = duration.get(ChronoUnit.WEEKS);
}
异常信息:
java.time.temporal.UnsupportedTemporalTypeException: Unsupported unit: Seconds
at java.time.LocalDate.until(LocalDate.java:1614)
at java.time.Duration.between(Duration.java:475)
at com.cgws.fintech.fundmall.bizfund.util.DateUtil.test(DateUtil.java:64)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
异常原因:是因为我between方法入参给的是LocalDate,需要LocalDateTime
异常解决:
@Test
public void test() {
LocalDate bgnDate = LocalDate.of(2020, 10, 10);
LocalDateTime bgnTime = bgnDate.atTime(0, 0, 0);
LocalDate endDate = LocalDate.of(2022, 5, 5);
LocalDateTime endTime = endDate.atTime(10, 5, 0);
Duration duration = Duration.between(bgnTime, endTime);
long toDays = duration.toDays();
long seconds = duration.get(ChronoUnit.SECONDS);
System.out.println("相差天数:"+toDays);
System.out.println("相差秒数:"+seconds);
}
运行结果:
@Test
public void test() {
LocalDate bgnDate = LocalDate.of(2020, 10, 10);
LocalDate endDate = LocalDate.of(2022, 5, 5);
long days = bgnDate.until(endDate, ChronoUnit.DAYS);
long years = bgnDate.until(endDate, ChronoUnit.YEARS);
long months = bgnDate.until(endDate, ChronoUnit.MONTHS);
long weeks = bgnDate.until(endDate, ChronoUnit.WEEKS);
System.out.println("相差年份:"+years);
System.out.println("相差月份:"+months);
System.out.println("相差天数:"+days);
System.out.println("相差周期:"+weeks);
}
运行结果:
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。