1

i have the following code that converts GMT time to local time, I took it from an answer here on StackOverflow, the problem is that this code return a false value of GMT time .

My GMT Time is : +3, but the code is using +2, it takes the GMT time from my device i guess, and my device's time is +3 GMT .

Here's the code :

        String inputText = "12:00";
    SimpleDateFormat inputFormat = new SimpleDateFormat
            ("kk:mm", Locale.US);
    inputFormat.setTimeZone(TimeZone.getTimeZone("GMT"));

    SimpleDateFormat outputFormat =
            new SimpleDateFormat("kk:mm");
    // Adjust locale and zone appropriately
    Date date = null;
    try {
        date = inputFormat.parse(inputText);
    } catch (ParseException e) {
        e.printStackTrace();
    }
    String outputText = outputFormat.format(date);
    Log.i("Time","Time Is: " + outputText);

The log returns : 14:00

Jaeger
  • 1,646
  • 8
  • 27
  • 59

1 Answers1

4

This is related to the date on which you are doing the conversion.

You are only specifying the hours and minutes, so the calculation is being done on January 1 1970. On that date, presumably, the GMT offset in your timezone is just 2 hours.

Specify the date too.


SimpleDateFormat inputFormat =
    new SimpleDateFormat("kk:mm", Locale.US);
inputFormat.setTimeZone(TimeZone.getTimeZone("GMT"));

SimpleDateFormat outputFormat =
    new SimpleDateFormat("yyyy/MM/dd kk:mm", Locale.US);
outputFormat.setTimeZone(TimeZone.getTimeZone("GMT"));

Date date = inputFormat.parse("12:00");

System.out.println("Time Is: " + outputFormat.format(date));

Ideone demo

Output:

Time Is: 1970/01/01 12:00

Additional code to show Daylight Savings Time / Summer Time impact:

SimpleDateFormat gmtFormat = new SimpleDateFormat("yyyy/MM/dd kk:mm", Locale.US);
gmtFormat.setTimeZone(TimeZone.getTimeZone("GMT"));

SimpleDateFormat finlandFormat = new SimpleDateFormat("yyyy/MM/dd kk:mm zzz", Locale.US);
finlandFormat.setTimeZone(TimeZone.getTimeZone("Europe/Helsinki"));

SimpleDateFormat plus3Format = new SimpleDateFormat("yyyy/MM/dd kk:mm zzz", Locale.US);
plus3Format.setTimeZone(TimeZone.getTimeZone("GMT+3"));

Date date = gmtFormat.parse("1970/01/01 12:00");
System.out.println("Time Is: " + gmtFormat.format(date));
System.out.println("Time Is: " + finlandFormat.format(date));
System.out.println("Time Is: " + plus3Format.format(date));

date = gmtFormat.parse("2016/04/22 12:00");
System.out.println("Time Is: " + gmtFormat.format(date));
System.out.println("Time Is: " + finlandFormat.format(date));
System.out.println("Time Is: " + plus3Format.format(date));

Output:

Time Is: 1970/01/01 12:00
Time Is: 1970/01/01 14:00 EET         <-- Eastern European Time
Time Is: 1970/01/01 15:00 GMT+03:00
Time Is: 2016/04/22 12:00
Time Is: 2016/04/22 15:00 EEST        <-- Eastern European Summer Time
Time Is: 2016/04/22 15:00 GMT+03:00
Andreas
  • 154,647
  • 11
  • 152
  • 247
Andy Turner
  • 137,514
  • 11
  • 162
  • 243
  • Not me, the output is : 1970/01/01 14:00, I don't think it's related to the year, As it takes the GMT offset from the device, So why it needs the Year too ? . – Jaeger Apr 22 '16 at 21:34
  • I'll bet you didn't know that Europe/London's GMT offset at Unix epoch (1970/01/01) was actually +1h because at that time the UK was in permanent daylight savings between [27 October 1968 and 31 October 1971](https://en.wikipedia.org/wiki/British_Summer_Time#Periods_of_deviation). On the yyyy/01/01, for every year since, it was +0h. Without knowing the year, you can't convert correctly, for reasons like this. – Andy Turner Apr 22 '16 at 21:37
  • Plus, daylight savings doesn't start and finish on the same day every year. – Andy Turner Apr 22 '16 at 21:44
  • It worked, it returned 15:00, seems like it was related to Year, I thought it will take the year from the device or use the device's GMT without "asking" for year. Thanks a lot ! :) – Jaeger Apr 22 '16 at 21:44
  • 1
    @AboHani If you are GMT+3, but only get offset of 2 hours for 1970/01/01, then you are in a Summer Time / Daylight Savings Time right now. E.g. Finland is +3 right now, but is +2 on New Years Day. I'll update Andy's answer to clarify, hope he's ok with that. – Andreas Apr 22 '16 at 21:44