1

i have a problem with the timezones of a android application.

in the following code is my example:

Calendar c1 = Calendar.getInstance(TimeZone.getTimeZone("America/Miami"));
    c2 = Calendar.getInstance(TimeZone.getDefault());
    c2.set(2012, Calendar.MAY, 1, 9, 0, 0);

    tv2.setText(c1.getTime().toString());
    tv3.setText(c2.getTime().toString());

when i start the app and look at the textViews i find: enter image description here

why they are both gmt+2 and the first not the miami timezone?

4
  • 1
    AFAIK, Your timezone will work only if your device has the particular timezone. Why don't you try using String[] getAvailableIDs() to check if you have the particular timezone. You could also try using TimeZone.getTimeZone("GMT+14:00") where u provide the timezone using GMT
    – Shubhayu
    Apr 16, 2012 at 7:44
  • okay, there is no miami so i changed it to "New_York" but its still not working.
    – gurehbgui
    Apr 16, 2012 at 7:51
  • also when i change it to TimeZone.getTimeZone("GMT-05:00") it does not work
    – gurehbgui
    Apr 16, 2012 at 7:54
  • Let me check and see if i can find some solution
    – Shubhayu
    Apr 16, 2012 at 7:58

2 Answers 2

6

I can duplicate what you're experiencing. You can get the correct time in Miami timezone by doing the following:

Calendar c1 = new GregorianCalendar(TimeZone.getTimeZone("GMT-4"));
int hour = c1.get(Calendar.HOUR);         
int minutes = c1.get(Calendar.MINUTE);     
int seconds = c1.get(Calendar.SECOND);

tv2.setText(String.format("%d:%d:%d", hour, minutes, seconds));

The output: 4:22:41

You can use either "America/New_York" or "GMT-4". However the last one as Chilledrat mention could have some issues with daylight saving.

2
  • Doesn't this approach have a problem when daylight saving changes?
    – Chilledrat
    Apr 16, 2012 at 11:00
  • Yes I think you're right. You can use "America/New_York", this should be the same timezone as Miami I believe. Calendar c1 = new GregorianCalendar(TimeZone.getTimeZone("America/New_York"));
    – Priebe
    Apr 16, 2012 at 11:42
6

Ok finally figured it out after a lot of trials. The problem that I observed is that, getTime() of Calendar seem to always return the date in the current TimeZone. I have still not found any data which says that this the way it works.

To get the data in the way you want, we need to introduce the SimpleDataFormat. this is what I did to get the data.

Calendar c1 = Calendar.getInstance(TimeZone.getTimeZone("America/New_York"), Locale.US);
Calendar c2 = Calendar.getInstance(TimeZone.getDefault());
c2.set(2012, Calendar.MAY, 1, 9, 0, 0);

SimpleDateFormat sdf = new SimpleDateFormat();
sdf.setCalendar(c1);
tv2.setText(sdf.format(c1.getTime()));
tv3.setText(c2.getTime().toString());

Here you get the output as per the defined TimeZone.

Note I also used the Locale as it is supposed to be a recommended practice. tell me if this works for you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.