Java 8 Convert given time and time zone to UTC time

We Are Going To Discuss About Java 8 Convert given time and time zone to UTC time. So lets Start this Java Article.

Java 8 Convert given time and time zone to UTC time

  1. Java 8 Convert given time and time zone to UTC time

    You are looking for ZonedDateTime class in Java8 – a complete date-time with time-zone and resolved offset from UTC/Greenwich. In terms of design, this class should be viewed primarily as the combination of a LocalDateTime and a ZoneId.

  2. Java 8 Convert given time and time zone to UTC time

    You are looking for ZonedDateTime class in Java8 – a complete date-time with time-zone and resolved offset from UTC/Greenwich. In terms of design, this class should be viewed primarily as the combination of a LocalDateTime and a ZoneId.

Solution 1

You are looking for ZonedDateTime class in Java8 – a complete date-time with time-zone and resolved offset from UTC/Greenwich. In terms of design, this class should be viewed primarily as the combination of a LocalDateTime and a ZoneId. The ZoneOffset is a vital, but secondary, piece of information, used to ensure that the class represents an instant, especially during a daylight savings overlap.

For example:

ZoneId australia = ZoneId.of("Australia/Sydney");
String str = "2015-01-05 17:00";
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm");
LocalDateTime localtDateAndTime = LocalDateTime.parse(str, formatter);
ZonedDateTime dateAndTimeInSydney = ZonedDateTime.of(localtDateAndTime, australia );

System.out.println("Current date and time in a particular timezone : " + dateAndTimeInSydney);

ZonedDateTime utcDate = dateAndTimeInSydney.withZoneSameInstant(ZoneOffset.UTC);

System.out.println("Current date and time in UTC : " + utcDate);

Original Author Mateusz Sroka Of This Content

Solution 2

An alternative to the existing answer is to setup the formatter with the appropriate time zone:

String input = "2015-01-05 17:00";
ZoneId zone = ZoneId.of("Australia/Sydney");

DateTimeFormatter fmt = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm").withZone(zone);
ZonedDateTime utc = ZonedDateTime.parse(input, fmt).withZoneSameInstant(UTC);

Since you want to interact with a database, you may need a java.sql.Timestamp, in which case you don’t need to explicitly convert to a UTC time but can use an Instant instead:

ZonedDateTime zdt = ZonedDateTime.parse(input, fmt);
Timestamp sqlTs = Timestamp.from(zdt.toInstant());

Original Author assylias Of This Content

Solution 3

   **// Refactored Logic**     

        ZoneId australia = ZoneId.of("Australia/Sydney");
        ZoneId utcZoneID= ZoneId.of("Etc/UTC");
        String ausTime = "2015-01-05 17:00";
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm");

        //converting in datetime of java8
        LocalDateTime ausDateAndTime = LocalDateTime.parse(ausTime, formatter);

        // DateTime With Zone
        ZonedDateTime utcDateAndTime = ausDateAndTime.atZone(utcZoneID);
        // output - 2015-01-05T17:00Z[Etc/UTC]

        // With Formating DateTime
        String utcDateTime = utcDateAndTime.format(formatter);
        // output - 2015-01-05 17:00

Original Author Mukesh Of This Content

Conclusion

So This is all About This Tutorial. Hope This Tutorial Helped You. Thank You.

Also Read,

Siddharth

I am an Information Technology Engineer. I have Completed my MCA And I have 4 Year Plus Experience, I am a web developer with knowledge of multiple back-end platforms Like PHP, Node.js, Python and frontend JavaScript frameworks Like Angular, React, and Vue.

Leave a Comment