代码实例
LocalDate localDate = LocalDate.now();
long currentTimeMillis = System.currentTimeMillis();
LocalDateTime currentTimeMillisLocalDateTime = LocalDateTime.ofEpochSecond(currentTimeMillis / 1000, 0, ZoneOffset.ofHours(8));
LocalDateTime dateCastToLocalDateTime = LocalDateTime.ofInstant(new Date().toInstant(), ZoneId.systemDefault());
Date date = new Date();
LocalDate dateCastToLocalDate = date.toInstant().atZone(ZoneOffset.ofHours(8)).toLocalDate();
Date localDateTimeCastDate = Date.from(LocalDateTime.now().atZone(ZoneId.systemDefault()).toInstant());
ZonedDateTime zonedDateTime = LocalDate.now().atStartOfDay(ZoneId.systemDefault());
Date localDateCastDate = Date.from(zonedDateTime.toInstant());
LocalDate parseDate = LocalDate.parse("2022-06-07");
LocalDate plusDate = localDate.plus(1, ChronoUnit.DAYS);
LocalDate minusDay = localDate.minus(1, ChronoUnit.DAYS);
int year = localDate.getYear();
int monthValue = localDate.getMonthValue();
boolean after = plusDate.isAfter(minusDay);
boolean before = parseDate.isBefore(minusDay);
boolean equal = parseDate.isEqual(minusDay);
System.out.println("currentTimeMillisLocalDateTime=" + currentTimeMillisLocalDateTime.toString());
System.out.println("dateCastToLocalDateTime=" + dateCastToLocalDateTime.toString());
System.out.println("dateCastToLocalDate=" + dateCastToLocalDate.toString());
System.out.println("localDateTimeCastDate=" + localDateTimeCastDate.toString());
System.out.println("localDateCastDate=" + localDateCastDate.toString());
System.out.println("parseDate=" + parseDate.toString());
System.out.println("plusDate=" + plusDate.toString());
System.out.println("minusDay=" + minusDay.toString());
System.out.println("year=" + year);
System.out.println("monthValue=" + monthValue);
System.out.println("after=" + after);
System.out.println("before=" + before);
System.out.println("equal=" + equal);
运行结果
currentTimeMillisLocalDateTime=2022-06-07T21:20:53
dateCastToLocalDateTime=2022-06-07T21:20:53.989
dateCastToLocalDate=2022-06-07
localDateTimeCastDate=Tue Jun 07 21:20:53 CST 2022
localDateCastDate=Tue Jun 07 00:00:00 CST 2022
parseDate=2022-06-07
plusDate=2022-06-08
minusDay=2022-06-06
year=2022
monthValue=6
after=true
before=false
equal=false