How to create Java time instant from pattern?

后端 未结 4 986
隐瞒了意图╮
隐瞒了意图╮ 2021-02-01 01:51

Consider a code:

TemporalAccessor date = DateTimeFormatter.ofPattern(\"yyyy-MM-dd\").parse(\"9999-12-31\");
Instant.from(date);

The last line t

4条回答
  •  走了就别回头了
    2021-02-01 02:07

    Either you are only interested in the date itself (31st of December 9999), in which case the appropriate type would be a LocalDate:

    LocalDate date = LocalDate.parse("9999-12-31");
    

    Or you do want an Instant, in which case you need to set a time and time zone, for example, 00:00 in Tokyo:

    Instant instant = date.atStartOfDay(ZoneId.of("Asia/Tokyo")).toInstant();
    

提交回复
热议问题