60

I have a time with string type like: "2015-01-05 17:00" and ZoneId is "Australia/Sydney".

How can I convert this time information to the corresponding to UTC time using Java 8 datetime API?

Also need to considering DST stuff.

lospejos
  • 1,976
  • 3
  • 19
  • 35
ttt
  • 3,934
  • 8
  • 46
  • 85
  • 2
    What do you mean by "UTC Time"? Do you want an Instant? or a java.sql.Date? Or a number of millis since the epoch? – assylias Jan 05 '16 at 08:26

3 Answers3

107

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);
sidgate
  • 14,650
  • 11
  • 68
  • 119
Mateusz Sroka
  • 2,255
  • 2
  • 17
  • 19
  • 2
    It seems this result is not what I want. I want to convert to the UTC time and store UTC time in my database. The ZonedDateTime is not what I want and can't map to DB column type. – ttt Jan 05 '16 at 07:53
  • 2
    Sorry I've added conversion to UTC in example. – Mateusz Sroka Jan 05 '16 at 08:07
  • 2
    Cool, it seems we must convert LocalDateTime to the ZonedDateTime and then we can convert between different timezone. Thanks. – ttt Jan 05 '16 at 08:53
  • @MateuszSroka - My time stamp has zero millis '2021-04-11 10:25:33.000'. This code truncates the zeros. How to show the zeros also? I posted a related question over here https://stackoverflow.com/questions/67102239/how-to-convert-a-datetime-string-into-utc-in-java – MasterJoe Apr 15 '21 at 05:21
23

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());
assylias
  • 321,522
  • 82
  • 660
  • 783
  • 2
    What is UTC variable on this context? – BOWS Mar 17 '17 at 13:48
  • 1
    It's a date/time, in UTC time zone, which corresponds to the same instant as "input" in Sydney. So in this case, it's going to be the same date but at 6am. – assylias Mar 17 '17 at 14:14
0
   **// 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
Mukesh
  • 921
  • 10
  • 23