本文介绍了转换"美国/洛杉矶"时区" PST"或QUOT; PDT"在Java ME的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的服务器发送我的格式为美国/洛杉矶的时区。在客户端,我有时间,我需要在该时区显示。答案将是PST或PDT,这取决于夏令在给定的时间。我如何做到这一点的转换?

我在的Java ME(黑莓4.7,是准确的),所以我不能使用乔达时间。

我将需要进行这种计算迅速了很多的日期(但只有一个时区),所以我不能只在服务器给我一个偏移,因为偏移量可能会根据日期变化。

编辑:让我重申这个问题,因为似乎有一些混乱。我给一个时区信息名字和日期。我想知道,从格林尼治标准​​时间在时区在该日期的偏移。答案将取决于日光节约时间变化。

作为额外的奖励,我想获得的TLA展现给用户(即PST或PDT),但这是次要的。

解决方案:我在这里总结的解决方案,因为它不是从下面的答案非常明确。这基本上做什么,我需要的,在J2ME:

  TimeZone的区= TimeZone.getTimeZone(美国/洛杉矶);
台历挂历= Calendar.getInstance();
calendar.setTimeZone(区);calendar.setTime(新日期(2011年,1,1,12,0,0));
的System.out.println(zone.getOffset(1,calendar.get(Calendar.YEAR),calendar.get(的Calendar.MONTH),calendar.get(Calendar.DAY_OF_MONTH),calendar.get(Calendar.DAY_OF_WEEK),日历。得到(Calendar.MILLISECOND)));
calendar.setTime(新日期(2011年,6,1,12,0,0));
的System.out.println(zone.getOffset(1,calendar.get(Calendar.YEAR),calendar.get(的Calendar.MONTH),calendar.get(Calendar.DAY_OF_MONTH),calendar.get(Calendar.DAY_OF_WEEK),日历。得到(Calendar.MILLISECOND)));


解决方案

可能这就是你想要的东西。这个使用标准的Java作品在我的机器上。

 日历日历= Calendar.getInstance();
        时区TZ = TimeZone.getTimeZone(美国/洛杉矶);
        calendar.setTimeZone(TZ);
         的System.out.println(UTC的偏移量=+ tz.getOffset(calendar.getTimeInMillis())/(60 * 60 * 1000)); //输出-8
        的System.out.println(tz.getDisplayName(Boolean.TRUE,0)); //输出PDT
        的System.out.println(tz.getDisplayName(Boolean.TRUE,1)); //输出太平洋夏令时
        的System.out.println(tz.getDisplayName(Boolean.FALSE,0)); //输出PST
        的System.out.println(tz.getDisplayName(Boolean.FALSE,1)); //输出太平洋标准时间

更新:OP上面说不适用于J2ME工作。谷歌搜索后,我发现here那的getOffset()实际上是getRawOffset()在J2ME中。但是,它看起来并不像它supposrts显示名。你唯一我觉得选择是使用通过IDS去getAvailableIds().

My server sends me time zones of the format "America/Los Angeles". On the client, I have a time that I need to display in that time zone. The answer will be "PST" or "PDT", depending on daylight savings at the given time. How do I do that conversion?

I'm on Java ME (Blackberry 4.7, to be accurate), so I can't use Joda Time.

I will need to make this calculation rapidly for a lot of dates (but only one time zone), so I can't just have the server send me an offset, because the offset might change depending on the date.

Edit: Let me restate the problem, because there seems to be some confusion. I am given a zoneinfo name and a date. I want to know the offset from GMT in that timezone at that date. The answer will vary depending on daylight savings time.

As an added bonus, I'd like to get the TLA to show to the user (i.e., "PST" or "PDT"), but that's secondary.

Solution: I'll summarize the solution here, because it isn't terribly clear from the answers below. This essentially does what I need, in J2ME:

TimeZone zone = TimeZone.getTimeZone("America/Los_Angeles");
Calendar calendar = Calendar.getInstance();
calendar.setTimeZone(zone);

calendar.setTime(new Date(2011, 1, 1, 12, 0, 0));
System.out.println(zone.getOffset(1, calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH), calendar.get(Calendar.DAY_OF_WEEK), calendar.get(Calendar.MILLISECOND)));
calendar.setTime(new Date(2011, 6, 1, 12, 0, 0));
System.out.println(zone.getOffset(1, calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH), calendar.get(Calendar.DAY_OF_WEEK), calendar.get(Calendar.MILLISECOND)));
解决方案

May be this is what you are wanting. This works on my machine using standard java.

        Calendar calendar = Calendar.getInstance();
        TimeZone tz = TimeZone.getTimeZone("America/Los_Angeles");
        calendar.setTimeZone(tz);
         System.out.println("Offset from UTC="+tz.getOffset(calendar.getTimeInMillis())/(60*60*1000)); //prints -8
        System.out.println(tz.getDisplayName(Boolean.TRUE, 0)); //prints PDT
        System.out.println(tz.getDisplayName(Boolean.TRUE, 1)); //prints Pacific Daylight Time
        System.out.println(tz.getDisplayName(Boolean.FALSE, 0));//prints PST
        System.out.println(tz.getDisplayName(Boolean.FALSE, 1));//prints Pacific Standard Time

Update: OP said the above does not work for j2ME. After googling I found here that getOffset() is actually getRawOffset() in j2me. However, it does not look like it supposrts displayName. Your only option I think is to go through the ids using getAvailableIds().

这篇关于转换"美国/洛杉矶"时区" PST"或QUOT; PDT"在Java ME的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-16 01:01