How to convert time value of time with UTC offset to GMT in java 7

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

I tried using the sampleDateFormat.parse() method. But I cannot get a TimeZone object for UTC offset.

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

Please help me convert UTC datetime to Greenwich Standard Time in Java 7.

+6
source share
3 answers

I assume that you have the original date as a string. Follow these steps:

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

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 } } 
+2
source

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 
+2
source

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 the java.time classes built into Java 8 and later. Most of the functionality has been ported to Java 6 and 7 in the ThreeTen-Backport project .

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

Parse the string as LocalDateTime because it lacks offset or zone information. Your input complies with ISO 8601 , so there is no need to specify a formatting template.

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

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

 OffsetDateTime odt = ldt.atOffset( offset ); 

From this, extract Instant , which is always in UTC .

 Instant instant = odt.toInstant(); 

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

In UTC , this is a different date, 20th place instead of 21st.

Watch the code in real time at IdeOne.com .


About java.time

The java.time framework is built into Java 8 and later. These classes supersede the nasty old legacy datetime classes such as java.util.Date , Calendar and SimpleDateFormat .

The Joda-Time project, now in maintenance mode , we recommend switching to the java.time classes.

To learn more, see the Oracle Tutorial . And search for qaru for many examples and explanations. JSR 310 specification .

Using the JDBC driver compatible with JDBC 4.2 or later, you can exchange java.time objects directly with your database. No need for strings or java.sql. * Classes.

Where to get java.time classes?

The ThreeTen-Extra project extends java.time with additional classes. This project is proof of possible future additions to java.time. Here you can find useful classes such as Interval , YearWeek , YearQuarter and more .

+2
source

Source: https://habr.com/ru/post/1013396/


All Articles