9

I need to get time difference between two dates in different time zones. Currently I am doing this:

Calendar c1=Calendar.getInstance(TimeZone.getTimeZone("EDT"));
Calendar c2=Calendar.getInstance(TimeZone.getTimeZone("GMT"));
String diff=((c2.getTimeInMillis()-c1.getTimeInMillis())/(1000*60*60))+" hours";
new AlertDialog.Builder(this).setMessage(diff).create().show();

I get 0 hours. What am I doing wrong?

gideon
  • 19,121
  • 11
  • 72
  • 112
sam
  • 91
  • 1
  • 2

3 Answers3

15

getTimeInMillis() returns the number of milliseconds since the epoch in UTC. In other words, the time zone is irrelevant to it.

I suspect you actually want:

long currentTime = System.currentTimeMillis();
int edtOffset = TimeZone.getTimeZone("EDT").getOffset(currentTime);
int gmtOffset = TimeZone.getTimeZone("GMT").getOffset(currentTime);
int hourDifference = (gmtOffset - edtOffset) / (1000 * 60 * 60);
String diff = hourDifference + " hours";
Jon Skeet
  • 1,335,956
  • 823
  • 8,931
  • 9,049
  • And what in case of Daylight Saving? will that handle it? – Gugan Oct 03 '13 at 10:40
  • 1
    @Gugan: I don't know what will happen if you use an abbreviation for the time zone name - but if you give it a full time zone ID such as `Europe/London`, that will indeed handle daylight saving. – Jon Skeet Oct 03 '13 at 10:45
  • Thank you Jon. I will check it. – Gugan Oct 03 '13 at 10:49
1

Jon is close, but due to character restrictions I can't edit his answer. This is the same code but with "EDT" changed to "EST" for Eastern Standard Time.

long currentTime = System.currentTimeMillis();
int edtOffset = TimeZone.getTimeZone("EST").getOffset(currentTime);
int gmtOffset = TimeZone.getTimeZone("GMT").getOffset(currentTime);
int hourDifference = (gmtOffset - edtOffset) / (1000 * 60 * 60);
String diff = hourDifference + " hours";

But this solution makes a major assumption that TimeZone.getAvailableIDs() has within it's string array both "EST" and "GMT". If that method doesn't contain those timezone strings it will come back as 0 offset.

Josh Lehman
  • 278
  • 5
  • 14
0

Here is my code two calculate the time difference between two different timezones. like current timezone(GMT+05:00) and foreign timezone(GMT+05:30).

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                LocalDate today = LocalDate.now();
                Duration timeDifference = Duration.between(today.atStartOfDay(ZoneId.of(foreigntimezone)), today.atStartOfDay(ZoneId.of(currenttimezone)));
                holder.textViewTimeDifference.setText(timeDifference.toString());
            } else {
                long now = System.currentTimeMillis();
                long diffMilliSeconds = TimeZone.getTimeZone(gmtReplaceUTC).getOffset(now) - TimeZone.getTimeZone(currentTimeZone).getOffset(now);
                if (diffMilliSeconds > 0) {
                    String timeDifference = formatTime(diffMilliSeconds);
                    holder.textViewTimeDifference.setText(timeDifference);
                } else {
                    String timeDifference = formatTime(-diffMilliSeconds);
                    holder.textViewTimeDifference.setText("-" + timeDifference);
                }
            }
}

Result: for above 26 API level (PT+30M) and for below (30:00)

Anwar Zahid
  • 164
  • 3
  • 10