Search code examples
androidtimezonelocale

Getting SimpleDateFormat for specific timezone


I am having a hard time displaying time for a specific timezone. I have a TV Guide like app that gets all start times of the programs in Unix epoch time from the server and I want the times to be shown in the time it would be in the Netherlands wherever the app user is and from whatever android device. After a bit of researching I would think it would work like this:

new SimpleDateFormat("HH:mm", new Locale("nl", "NL")).format(new Date(time*1000));

where time is a epoch time for example 1370497200

This is 07:40 in the Netherlands but it seems it just takes the timezone of the device you are using. Tested it on a Samsung Galaxy Tab 2.0

What am I doing wrong?

A related question: According to http://developer.android.com/reference/java/util/Locale.html I can't assume the nl_NL Locale is available on every device, how would I solve this?

Sidenote: http://www.epochconverter.com/ is a nice way to quickly see what time a certain epoch time is.


Solution

  • Use setTimeZone():

    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("HH:mm", new Locale("nl", "NL"));
    simpleDateFormat.setTimeZone(TimeZone.getTimeZone("Europe/Amsterdam"));
    

    A related question: According to http://developer.android.com/reference/java/util/Locale.html I can't assume the nl_NL Locale is available on every device, how would I solve this?

    Are you talking about the UI here? If so, then you can not solve it. Only Google and/or OEMs can add new locales to the system.