Convert A DateTime With Given Offset to UTC Java

Lucifer Morningstar :

Given that I have String "2019-11-05/23:00" and an offset "+07:00",

How would I go About Converting It to LocalDateTime UTC in Java(8).

My Current Code

@GetMapping("postDate/{date}")
public void testPost(@RequestParam("timezone") String timeZone, @PathVariable String date) {
    String format = "yyyy-MM-dd-HH:mm";
    String offset = timeZone;
    System.out.println(date);
    LocalDateTime timeWithOffset = LocalDateTime.parse(date,
                                                       DateTimeFormatter.ofPattern(format));

    System.out.println("\n\n\n" + timeWithOffset + "\n\n\n");

    // Cant Figure Out to get LocalDateTime timeInUTC
}

My Request From Postman

http://localhost:8080/postDate/2019-11-05-23:00?timezone=+07:00

deHaar :

You can make use of the zone- and offset-aware classes in java.time. You get an offset (I suggest to name the parameter accordingly, because an offset is not a time zone) and the date time, which is sufficient to get the same instant converted into different time zones or offsets.

Have a look at this example and read the comments:

public static void main(String[] args) {
    // this simulates the parameters passed to your method
    String offset = "+07:00";
    String date = "2019-11-05/23:00";

    // create a LocalDateTime using the date time passed as parameter
    LocalDateTime ldt = LocalDateTime.parse(date,
                                            DateTimeFormatter.ofPattern("yyyy-MM-dd/HH:mm"));
    // parse the offset
    ZoneOffset zoneOffset = ZoneOffset.of(offset);
    // create an OffsetDateTime using the parsed offset
    OffsetDateTime odt = OffsetDateTime.of(ldt, zoneOffset);
    // print the date time with the parsed offset
    System.out.println(zoneOffset.toString()
            + ":\t" + odt.format(DateTimeFormatter.ISO_OFFSET_DATE_TIME));
    // create a ZonedDateTime from the OffsetDateTime and use UTC as time zone
    ZonedDateTime utcZdt = odt.atZoneSameInstant(ZoneOffset.UTC);
    // print the date time in UTC using the ISO ZONED DATE TIME format
    System.out.println("UTC:\t"
            + utcZdt.format(DateTimeFormatter.ISO_ZONED_DATE_TIME));
    // and then print it again using your desired format
    System.out.println("UTC:\t"
            + utcZdt.format(DateTimeFormatter.ofPattern("yyyy-MM-dd-HH:mm")));
}

The output on my system is:

+07:00: 2019-11-05T23:00:00+07:00
UTC:    2019-11-05T16:00:00Z
UTC:    2019-11-05-16:00

For the reverse case (you get a UTC time and an offset and want the OffsetDateTime), this may work:

public static void main(String[] args) {
    // this simulates the parameters passed to your method
    String offset = "+07:00";
    String date = "2019-11-05/16:00";
    // provide a pattern
    String formatPattern = "yyyy-MM-dd/HH:mm";
    // and create a formatter with it
    DateTimeFormatter dtf = DateTimeFormatter.ofPattern(formatPattern);
    // then parse the time to a local date using the formatter
    LocalDateTime ldt = LocalDateTime.parse(date, dtf);
    // create a moment in time at the UTC offset (that is just +00:00)
    Instant instant = Instant.ofEpochSecond(ldt.toEpochSecond(ZoneOffset.of("+00:00")));
    // and convert the time to one with the desired offset
    OffsetDateTime zdt = instant.atOffset(ZoneOffset.of(offset));
    // finally print it using your formatter
    System.out.println("UTC:\t" + ldt.format(dtf));
    System.out.println(zdt.getOffset().toString()
            + ": " + zdt.format(DateTimeFormatter.ofPattern(formatPattern)));
}

The output is this:

UTC:    2019-11-05/16:00
+07:00: 2019-11-05/23:00

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=322845&siteId=1