jdk18-日期-API-常用方法

7次阅读

共计 2898 个字符,预计需要花费 8 分钟才能阅读完成。

Date to LocalDateTime

  • 方法 1
Date d = new Date();
LocalDateTime ldt = d.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime();
  • 方法 2
Date d = new Date();
LocalDateTime ldt = LocalDateTime.ofInstant(d.toInstant(), ZoneId.systemDefault());

Date to LocalDate

  • 方法 1
Date d = new Date();
LocalDate ld = d.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime().toLocalDate();
  • 方法 2
Date d = new Date();
LocalDate ld = LocalDateTime.ofInstant(d.toInstant(), ZoneId.systemDefault()).toLocalDate();

LocalDateTime to Date

  • 方法 1
LocalDateTime now = LocalDateTime.now();
Date.from(now.atZone(ZoneId.systemDefault()).toInstant());

LocalDate to Date

  • 方法 1
LocalDate now = LocalDate.now();
        Date from = Date.from(now.atStartOfDay(ZoneId.systemDefault()).toInstant());

处理不同的时区(ZoneId)

LocalDate now = LocalDate.now();
ZonedDateTime zonedDateTime = now.atStartOfDay(ZoneId.of("Europe/Rome"));
LocalDateTime now = LocalDateTime.now();
ZonedDateTime zonedDateTime1 = now.atZone(ZoneId.of("Europe/Rome"));

计算时间差(ChronoUnit | Period)

// 2019-10-19
LocalDate now = LocalDate.now();
LocalDate of = LocalDate.of(2017, 6, 12);
// -859;后一个数减前一个数
ChronoUnit.DAYS.between(now, of);
// -2
ChronoUnit.YEAR.between(now, of);
// -28
ChronoUnit.MONTHS.between(now, of);
// UnsupportedTemporalTypeException
ChronoUnit.HOURS.between(now, of);
// 2019-10-19T17:27:34
LocalDateTime now = LocalDateTime.now();
LocalDateTime of = LocalDateTime.of(2016, 12, 12, 12, 12, 12);
// -24989
ChronoUnit.HOURS.between(now, of);
// -1499355
CHronoUnit.MINUTES.between(now, of);

格式化(DateTimeFormatter,DateTimeFormatterBuilder)

LocalDateTime now = LocalDateTime.now();
// 2019-10-19
now.format(DateTimeFormatter.ISO_DATE);
// 20191019
now.format(DateTimeFormatter.BASIC_ISO_DATE);
// 19/33/19 17:10:17
now.format(DateTimeFormatter.ofPattern("yy/mm/dd HH:MM:ss"));
// 2019. 十月 19
DateTimeFormatter formatter = new DateTimeFormatterBuilder()
                .appendText(ChronoField.YEAR)
                .appendLiteral(".")
                .appendText(ChronoField.MONTH_OF_YEAR)
                .appendLiteral(" ")
                .appendText(ChronoField.DAY_OF_MONTH)
                .toFormatter(Locale.CHINA);
now.format(formatter)

解析(DateTimeFormatter,DateTimeFormatterBuilder)

// 2019-10-19T01:12:12
LocalDateTime.parse("2019-10-19T01:12:12");
// 2019-10-19T17:33:17
LocalDateTime.parse("19/33/19 17:10:17", DateTimeFormatter.ofPattern("yy/mm/dd HH:MM:ss");
// 2019-10-19
DateTimeFormatter formatter = new DateTimeFormatterBuilder()
                .appendText(ChronoField.YEAR)
                .appendLiteral(".")
                .appendText(ChronoField.MONTH_OF_YEAR)
                .appendLiteral(" ")
                .appendText(ChronoField.DAY_OF_MONTH)
                .toFormatter(Locale.CHINA);
LocalDate.parse("2019. 十月 19", formatter)

时间调整(TemporalAdjusters | TemporalAdjuster)

// 2019-10-19
LocalDate now = LocalDate.now();
// 2019-10-12
now.with(TemporalAdjusters.previous(DayOfWeek.SATURDAY));
// 2019-10-19
now.with(TemporalAdjusters.previousOrSame(DayOfWeek.SATURDAY))

获取年月日(ChronoField)

  • 方法 1
LocalDate now = LocalDate.now();
now.getYear();
now.getMonth().getValue();
now.getDayOfMonth();
  • 方法 2
LocalDateTime now = LocalDateTime.now();
int year = now.get(ChronoField.YEAR);
int month = now.get(ChronoField.MONTH_OF_YEAR);
int days = now.get(ChronoField.DAY_OF_MONTH);

正文完
 0