-1

I want to convert datetime-local which is from html to UTC time:

html:

<label>Start Time:</label><input type="datetime-local" name="start_time" />

java:

request.getParameter("start_time") // print out ex:2021-01-07T10:42

that is local time(GMT+8), and I want to convert it to UTC string time

ex: convert to -> 2021-01-07T02:42

I search much resource but still not understand how to do.

Arvind Kumar Avinash
  • 71,965
  • 6
  • 74
  • 110
Howard
  • 143
  • 1
  • 1
  • 12
  • You may find the answer from this link https://stackoverflow.com/questions/37390080/convert-local-time-to-utc-and-vice-versa – Hkachhia Jan 08 '21 at 08:30
  • It's generally not a good idea to rely on the Date and Calendar classes. They are very error prone and they've ben deprecated for a long, long time. If you are on a Java version before 1.8 it's far more reliable to use the Joda-Time library. https://www.joda.org/joda-time/ On Java 1.8 and up this is not needed since ava.time (JSR-310) is already present there. – Erik Jan 08 '21 at 08:50
  • What did your search bring up? Mine found [How to parse/format dates with LocalDateTime? (Java 8)](https://stackoverflow.com/q/22463062/10819573) and [Convert LocalDateTime to LocalDateTime in UTC](https://stackoverflow.com/q/34626382/10819573). Please read through what you find and then tell us precisely what you’re still missing. And then we’ll be here to help. – Arvind Kumar Avinash Jan 09 '21 at 19:05

2 Answers2

1

In ISO-8601 format you can just append the timezone offset to the string and java.time.Instant knows how to deal with that like so:

Instant result = Instant.parse("2021-01-08T09:36:29+08:00");
System.out.println(result);

This prints out: 2021-01-08T01:36:29Z

Erik
  • 74
  • 6
  • 2
    Which Java version are you using? On my Java 11 I get `java.time.format.DateTimeParseException: Text '2021-01-08T09:36:29+08:00' could not be parsed at index 19`. – Ole V.V. Jan 08 '21 at 08:48
  • I ran this on Java 14, but AFAIK this should not have any impact on the `java.time.Instant` class. – Erik Jan 08 '21 at 08:55
  • You can also try parsing with `java.time.LocalDateTime#parse` there you can specify a DateTimeFormat instance to match your parsing needs. – Erik Jan 08 '21 at 08:58
  • sorry how could I get java.time.Instant library – Howard Jan 08 '21 at 09:12
  • 1
    It's part of the JDK. Since Java 1.8. – Erik Jan 08 '21 at 09:28
  • oh no my jdk is 1.7 – Howard Jan 08 '21 at 09:40
  • 1
    For Java 1.7 use [ThreeTen Backport](https://www.threeten.org/threetenbp/). It’s the same code, only ported to Java 1.6 and 1.7. – Ole V.V. Jan 08 '21 at 10:23
  • 1
    I also occur the problem:java.time.format.DateTimeParseException: Text '2021-01-08T09:36:29+08:00' could not be parsed at index 19 – Howard Jan 08 '21 at 11:33
  • The ThreeTen websie does mention this. "Formatting and parsing often depends on data only available in Java SE 8. Zone id and text parsing is significantly less powerful." Alternatively on Java 1.7 you can try using the [Joda-Time](https://www.joda.org/joda-time/) library. The API is slightly different but it's still a hell of a lot more robust then Date and Calendar. – Erik Jan 08 '21 at 13:36
0

java.time through ThreeTen Backport

java.time is the modern Java date and time API. I recommend that we use it for our date and time work. It was the successor of Joda-Time and came out with Java 1.8 in 2014. It has also been backported to Java 1.6 and 1.7 in the ThreeTen Backport. Using ThreTen Backport we may do:

    String startTime = "2021-01-07T10:42";
    OffsetDateTime utcTime = LocalDateTime.parse(startTime)
            .atZone(ZoneId.systemDefault())
            .toOffsetDateTime()
            .withOffsetSameInstant(ZoneOffset.UTC);
    System.out.println(utcTime);

I ran this code on Java 1.7 in Asia/Shanghai time zone (at offset +08:00). Then the output is:

2021-01-07T02:42Z

The time agrees with what you wanted. And it’s in UTC, denoted by the trailing Z.

If you want to make explicit which time zone to convert from, instead of ZoneId.systemDefault() use something like ZoneId.of("Asia/Hong_Kong"). It’s better to give the relevant time zone ID in the region/city format than a mere GMT offset.

Why would you want to use an external library for your conversion? java.time is the future-proof library for date and time. Once you move on to Java 8, 9, 10, 11, 12, 13, 14, 15, 16 or later, change your imports to use the built-in java.time, test again and your code is top modern. Note how the code is explicit about converting to UTC, there’s no surprise what’s going on. java.time is so nice to work with.

Links

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161