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.
I will assume you have the original date as a string. Do the following:
SimpleDateFormat
and set the timezone to "GMT+14"Date
objectSimpleDateFormat
to "UTC" (or use a different SimpleDateFormat
instance)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
}
}
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
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With