How to Convert a Date time value with UTC offset into GMT in java 7

11.6k views Asked by At

I have a date time value 2016-12-21T07:48:36 with an offset of UTC+14. How to convert the datetime into equivalent standard GMT time.

I tried with sampleDateFormat.parse() method.But, I am not able to get the TimeZone object for UTC offset like.

sampleDateFormat.setTimeZone(TimeZone.getTimeZone("UTC+14:00"))

Please help me to convert the UTC datetime into standard GMT time in Java 7.

3

There are 3 answers

4
Basil Bourque On

tl;dr

LocalDateTime.parse( "2016-12-21T07:48:36" )        // Parse as a `LocalDateTime` given the lack of an offset or zone. *Not* an actual moment, only a rough approximation of potential moments along a range of about 26-27 hours.
             .atOffset( ZoneOffset.ofHours( 14 ) )  // Assign an offset-from-UTC as context, giving meaning to determine an actual point on the timeline.
             .toInstant()                           // Renders `Instant` object in UTC.

java.time

The modern way is with the java.time classes built into Java 8 and later. Much of the functionality is back-ported to Java 6 & 7 in the ThreeTen-Backport project.

ZoneOffset offset = ZoneOffset.ofHours( 14 ); // fourteen hours ahead of UTC.

Parse the string as a LocalDateTime as it lacks any info about offset or zone. Your input is in standard ISO 8601 format, so no need to specify a formatting pattern.

LocalDateTime ldt = LocalDateTime.parse( "2016-12-21T07:48:36" );

Apply the offset to the local date-time to get an OffsetDateTime an object.

OffsetDateTime odt = ldt.atOffset( offset );

From that, extract an Instant which is always in UTC.

Instant instant = odt.toInstant();

instant.toString(): 2016-12-20T17:48:36Z

In UTC, the value is a different date, the 20th instead of 21st.

See live code at IdeOne.com.


About java.time

The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date, Calendar, & SimpleDateFormat.

The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.

To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.

Using a JDBC driver compliant with JDBC 4.2 or later, you may exchange java.time objects directly with your database. No need for strings nor java.sql.* classes.

Where to obtain the java.time classes?

The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval, YearWeek, YearQuarter, and more.

3
Grodriguez On

I will assume you have the original date as a string. Do the following:

  • Create a SimpleDateFormat and set the timezone to "GMT+14"
  • Parse the string value. You get a Date object
  • Set the timezone of the SimpleDateFormat to "UTC" (or use a different SimpleDateFormat instance)
  • Format the date (if you want the result as a string as well)

Example:

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;

public class ConvertToUTC {
  public static void main(String[] args) throws Exception {

      String dateval = "2016-12-21T07:48:36";
      DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
      df.setTimeZone(TimeZone.getTimeZone("GMT+14"));
      Date date = df.parse(dateval);

      System.out.println(df.format(date)); // GMT+14

      df.setTimeZone(TimeZone.getTimeZone("UTC"));
      System.out.println(df.format(date)); // UTC
  }
}
0
Sxilderik On

use "GMT+14:00" instead of "UTC+14:00"

SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd'T'hh:mm:ss");
f.setTimeZone(TimeZone.getTimeZone("GMT+14:00"));
final Date d = f.parse("2016-12-21T07:48:36");
f.setTimeZone(TimeZone.getTimeZone("UTC"));
System.out.println(f.format(d)); // -> 2016-12-20T05:48:36