Getting SimpleDateFormat for specific timezone

五迷三道 提交于 2020-01-04 07:53:46

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/16964910/getting-simpledateformat-for-specific-timezone

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!