美文网首页
2020-12-08 Java 日期相关操作API

2020-12-08 Java 日期相关操作API

作者: 华清居士 | 来源:发表于2020-12-08 17:52 被阅读0次

1、 Date 之 LocalDate
LocalDate localDate = LocalDate.parse("2020-12-07");
  DateTimeFormatter pattern = DateTimeFormatter.ofPattern("yyyy年MM月dd日");
  System.out.println(LocalDate.parse("2020-10-09").format(pattern));
2、Time之LocalTime
LocalTime localTime = LocalTime.parse("07:43:53");
3、DateTime之 LocalDateTime
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd hh:mm:ss");
LocalDate localDate = LocalDate.parse("2019-12-07 07:43:53",formatter);

date time -------- to String

LocalTime localTime = LocalTime.now();
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("hh:mm:ss");
String format4 = localTime.format(formatter);

LocalDateTime localDateTime = LocalDateTime.now();
DateTimeFormatter formatter2 = DateTimeFormatter.ofPattern("yyyy-MM-dd hh:mm:ss");
String format5 = localDateTime.format(formatter2);

date 《-----------》 LocalDateTime
1、
Date todayDate = new Date();
LocalDateTime ldt = todayDate.toInstant()
.atZone( ZoneId.systemDefault() )
.toLocalDateTime();
2、
LocalDateTime localDateTime = LocalDateTime.now();、
Date date = Date.from( localDateTime.atZone( ZoneId.systemDefault()).toInstant());

  1. LocalDate 转 Date

ZonedDateTime zdt = localDate.atStartOfDay(ZoneId.systemDefault());

    Date date = Date.from(zdt.toInstant());

//LocalDateTime -> Date
public static Date asDate(LocalDateTime localDateTime) {
    return Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant());
}

转毫秒
LocalDateTime.now().toInstant(ZoneOffset.of("+8")).toEpochMilli();

相关文章

网友评论

      本文标题:2020-12-08 Java 日期相关操作API

      本文链接:https://www.haomeiwen.com/subject/dhdqgktx.html