Convert Date/Time for given Timezone - java

前端 未结 16 2427
孤城傲影
孤城傲影 2020-11-22 12:36

I want to convert this GMT time stamp to GMT+13:

2011-10-06 03:35:05

I have tried about 100 different combinations of DateFormat, TimeZone,

16条回答
  •  长情又很酷
    2020-11-22 13:05

    The solution is actually quite simple (pure, simple Java):

    System.out.println(" NZ Local Time: 2011-10-06 03:35:05");
    DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
    LocalDateTime localNZ = LocalDateTime.parse("2011-10-06 03:35:05",formatter);
    ZonedDateTime zonedNZ = ZonedDateTime.of(localNZ,ZoneId.of("+13:00"));
    LocalDateTime localUTC = zonedNZ.withZoneSameInstant(ZoneId.of("UTC")).toLocalDateTime();
    System.out.println("UTC Local Time: "+localUTC.format(formatter));
    

    OUTPUT IS:

     NZ Local Time: 2011-10-06 03:35:05
    UTC Local Time: 2011-10-05 14:35:05
    

提交回复
热议问题