Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java Convert GMT/UTC to Local time doesn't work as expected

In Order to show a reproducible scenario, I am doing the following

  1. Get the current system time (local time)

  2. Convert Local time to UTC // Works Fine Till here

  3. Reverse the UTC time, back to local time. Followed 3 different approaches (listed below) but all the 3 approaches retains the time in UTC only.

    {

    long ts = System.currentTimeMillis(); Date localTime = new Date(ts); String format = "yyyy/MM/dd HH:mm:ss"; SimpleDateFormat sdf = new SimpleDateFormat (format);  // Convert Local Time to UTC (Works Fine)  sdf.setTimeZone(TimeZone.getTimeZone("UTC")); Date gmtTime = new Date(sdf.format(localTime)); System.out.println("Local:" + localTime.toString() + "," + localTime.getTime() + " --> UTC time:" + gmtTime.toString() + "-" + gmtTime.getTime());  // Reverse Convert UTC Time to Locale time (Doesn't work) Approach 1 sdf.setTimeZone(TimeZone.getDefault());         localTime = new Date(sdf.format(gmtTime)); System.out.println("Local:" + localTime.toString() + "," + localTime.getTime() + " --> UTC time:" + gmtTime.toString() + "-" + gmtTime.getTime());  // Reverse Convert UTC Time to Locale time (Doesn't work) Approach 2 using DateFormat DateFormat df = new SimpleDateFormat (format); df.setTimeZone(TimeZone.getDefault()); localTime = df.parse((df.format(gmtTime))); System.out.println("Local:" + localTime.toString() + "," + localTime.getTime() + " --> UTC time:" + gmtTime.toString() + "-" + gmtTime.getTime());  // Approach 3 Calendar c = new GregorianCalendar(TimeZone.getDefault()); c.setTimeInMillis(gmtTime.getTime()); System.out.println("Local Time " + c.toString()); 

    }

like image 427
Vinod Jayachandran Avatar asked Oct 15 '13 07:10

Vinod Jayachandran


People also ask

How can I get the current date and time in UTC or GMT in Java?

SimpleDateFormat f = new SimpleDateFormat("yyyy-MMM-dd HH:mm:ss"); f. setTimeZone(TimeZone. getTimeZone("UTC")); System. out.

How do you convert UTC to normal time?

Add the local time offset to the UTC time. For example, if your local time offset is -5:00, and if the UTC time is shown as 11:00, add -5 to 11. The time setting when adjusted for offset is 06:00 (6:00 A.M.). Note The date also follows UTC format.

What is UTC TimeZone in Java?

UTC stands for Co-ordinated Universal Time. It is time standard and is commonly used across the world. All timezones are computed comparatively with UTC as offset.


2 Answers

I also recommend using Joda as mentioned before.

Solving your problem using standard Java Date objects only can be done as follows:

    // **** YOUR CODE **** BEGIN ****     long ts = System.currentTimeMillis();     Date localTime = new Date(ts);     String format = "yyyy/MM/dd HH:mm:ss";     SimpleDateFormat sdf = new SimpleDateFormat(format);      // Convert Local Time to UTC (Works Fine)     sdf.setTimeZone(TimeZone.getTimeZone("UTC"));     Date gmtTime = new Date(sdf.format(localTime));     System.out.println("Local:" + localTime.toString() + "," + localTime.getTime() + " --> UTC time:"             + gmtTime.toString() + "," + gmtTime.getTime());      // **** YOUR CODE **** END ****      // Convert UTC to Local Time     Date fromGmt = new Date(gmtTime.getTime() + TimeZone.getDefault().getOffset(localTime.getTime()));     System.out.println("UTC time:" + gmtTime.toString() + "," + gmtTime.getTime() + " --> Local:"             + fromGmt.toString() + "-" + fromGmt.getTime()); 

Output:

Local:Tue Oct 15 12:19:40 CEST 2013,1381832380522 --> UTC time:Tue Oct 15 10:19:40 CEST 2013,1381825180000 UTC time:Tue Oct 15 10:19:40 CEST 2013,1381825180000 --> Local:Tue Oct 15 12:19:40 CEST 2013-1381832380000 
like image 62
trylimits Avatar answered Sep 29 '22 04:09

trylimits


You have a date with a known timezone (Here Europe/Madrid), and a target timezone (UTC)

You just need two SimpleDateFormats:

         long ts = System.currentTimeMillis();         Date localTime = new Date(ts);          SimpleDateFormat sdfLocal = new SimpleDateFormat ("yyyy/MM/dd HH:mm:ss");         sdfLocal.setTimeZone(TimeZone.getTimeZone("Europe/Madrid"));          SimpleDateFormat sdfUTC = new SimpleDateFormat ("yyyy/MM/dd HH:mm:ss");         sdfUTC.setTimeZone(TimeZone.getTimeZone("UTC"));          // Convert Local Time to UTC         Date utcTime = sdfLocal.parse(sdfUTC.format(localTime));         System.out.println("Local:" + localTime.toString() + "," + localTime.getTime() + " --> UTC time:" + utcTime.toString() + "-" + utcTime.getTime());          // Reverse Convert UTC Time to Locale time         localTime = sdfUTC.parse(sdfLocal.format(utcTime));         System.out.println("UTC:" + utcTime.toString() + "," + utcTime.getTime() + " --> Local time:" + localTime.toString() + "-" + localTime.getTime()); 

So after see it working you can add this method to your utils:

     public Date convertDate(Date dateFrom, String fromTimeZone, String toTimeZone) throws ParseException {         String pattern = "yyyy/MM/dd HH:mm:ss";         SimpleDateFormat sdfFrom = new SimpleDateFormat (pattern);         sdfFrom.setTimeZone(TimeZone.getTimeZone(fromTimeZone));          SimpleDateFormat sdfTo = new SimpleDateFormat (pattern);         sdfTo.setTimeZone(TimeZone.getTimeZone(toTimeZone));          Date dateTo = sdfFrom.parse(sdfTo.format(dateFrom));         return dateTo;     } 
like image 22
albfan Avatar answered Sep 29 '22 05:09

albfan