Java 8 Offset Date Parsing

前端 未结 2 1219
盖世英雄少女心
盖世英雄少女心 2021-01-11 23:01

I need to parse a String in the following format 2015-01-15-05:00 to LocalDate(or smth else) in UTC. The problem is that the following code:

Sys         


        
2条回答
  •  忘掉有多难
    2021-01-11 23:18

    Seems like I've found a solution. Here it is:

    TemporalAccessor temporalAccessor = DateTimeFormatter.ISO_OFFSET_DATE.parse("2015-01-15-05:00");
    ZonedDateTime zonedDateTime = ZonedDateTime.of(LocalDate.from(temporalAccessor), LocalTime.MAX, ZoneId.from(temporalAccessor));
    System.out.println(zonedDateTime.withZoneSameInstant(ZoneOffset.UTC).toLocalDate());
    

提交回复
热议问题