3

我有一个Date来自今年夏天夏令时的 Java。例如:

2009 年 6 月 1 日上午 06:00 PDT

我的问题是,如何将此日期显示为我的本地时区和当前的夏令时模式(在我的情况下为太平洋标准时间)?

太平洋标准时间 2009 年 6 月 1 日上午 05:00

Java 的Date.toString()并以原始夏令时模式SimpleDateFormat显示日期。例子:

System.out.println(new Date(1243861200000L));

输出:

2009 年 6 月 1 日星期一 06:00:00 PDT

DateFormat dateFormat = new SimpleDateFormat("MMM dd, yyyy hh:mm aa zzz");
dateFormat.setTimeZone(TimeZone.getTimeZone("PST"));
System.out.println(dateFormat.format(new Date(1243861200000L)));

输出:

2009 年 6 月 1 日上午 06:00 PDT

我真正想看到的是太平洋标准时间上午 5:00(相当于太平洋夏令时间上午 6:00)。有没有办法强制它使用另一种夏令时模式?

跟进:顺便说一下,这是 Windows XP 的行为。在 6 月 1 日上午 6 点创建的文件(通过资源管理器、命令提示符等)将在冬季显示为 6 月 1 日上午 5 点。

4

4 回答 4

2

PST/PDT 全年变化。Java 尝试确定哪一个在给定日期受到影响。随着法律和地区的变化,这件事显然会发生变化。您可以使用绝对时区吗?在 GMT 中指定偏移量将在同一年左右。

    DateFormat dateFormat = new SimpleDateFormat("MMM dd, yyyy hh:mm aa zzz");
    dateFormat.setTimeZone(TimeZone.getTimeZone("GMT-07:00"));
    System.out.println(dateFormat.format(new Date(1243861200000L)));

Jun 01, 2009 06:00 AM GMT-07:00
于 2009-11-19T03:05:22.003 回答
2

我认为没有办法强制将DateFormat格式TimeZone应用于不存在的时间。PST 仅适用于冬季,PDT 仅适用于夏季。

TimeZone我用s 返回的所有 s 格式化了这个日期TimeZone.getAvailableIDs(),并找到了两个在那个特定时间返回给我 PST:Pacific/Pitcairn 和 SystemV/PST8。您可以尝试使用其中一个,但我不知道还有哪些其他规则适用于这些时区对象。

如果您只关心使用该偏移量获取时间,则可以将GMT-8其用作您的 TimeZone。但是,该字符串将显示在您的格式化日期字符串中,而不是 PST。

更新: 这是一种采用任何时区并让它忽略所有夏令时规则的方法。您可以抓取一个TimeZone对象,然后抓取另一个TimeZone.getTimeZone()对象。在第二个对象上,将Idand设置rawOffset为第一个对象的相应对象TimeZone

例如:

    DateFormat dateFormat = new SimpleDateFormat("MMM dd, yyyy hh:mm aa zzz");
    TimeZone defaultTimeZone = TimeZone.getDefault();
    TimeZone timeZone = TimeZone.getTimeZone("");
    timeZone.setID(defaultTimeZone.getID());
    timeZone.setRawOffset(defaultTimeZone.getRawOffset());
    dateFormat.setTimeZone(timeZone);
    System.out.println(dateFormat.format(new Date(1243861200000L)));

这将准确打印出您要查找的内容。

于 2009-11-19T02:38:10.257 回答
1

这可能会奏效:

    /*
     * Create a date that represents 1,243,861,200,000 milliseconds since
     * Jan 1, 1970
     */
    Date date = new Date(1243861200000L);

    /*
     * Print out the date using the current system's default format and time
     * zone. In other words, this will print differently if you set the
     * operating zone's time zone differently.
     */
    System.out.println(date);

    /*
     * Specify the format and timezone to use
     */
    DateFormat dateFormat = new SimpleDateFormat(
            "MMM dd, yyyy hh:mm aa zzz");
    TimeZone pstTZ = TimeZone.getTimeZone("PST");
    dateFormat.setTimeZone(pstTZ);
    System.out.println(dateFormat.format(date));

    /*
     * It looks like what you want is to actually display a different date
     * (different # milliseconds since epoch) in the summer months. To do that, you
     * need to change the date by subtracting an hour.
     */
    if(pstTZ.inDaylightTime(date)){
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(Calendar.HOUR, -1);
        date = cal.getTime();
    }
    //now this should always print "5:00". However, it will show as 5:00 PDT in summer and 5:00 PST in the winter which are actually 2 different dates. So, I think you might need to think about exactly what it is you're trying to achieve. 
    System.out.println(dateFormat.format(date));
于 2009-11-19T02:39:36.353 回答
0
DateFormat dateFormat = new SimpleDateFormat("MMM dd, yyyy hh:mm aa zzz"); 
dateFormat.setTimeZone(TimeZone.getTimeZone("Pacific/Pitcairn")); 
System.out.println(dateFormat.format(new Date(1243861200000L)));

附加说明:zzz 将打印出 PST,而 z 将打印出 GMT-08:00。TimeZone.getTimeZone("EST") 适用于东部标准时间,让我认为这是 TimeZone.getTimeZone("PST") 的错误。

我建议使用预定义的时区,因为它们可能会造成混淆:

public static final TimeZone ZONE_GREENWICH_MEAN_TIME = TimeZone.getTimeZone("GMT");
public static final TimeZone ZONE_EASTERN_DAYLIGHT_TIME = TimeZone.getTimeZone("America/New_York"); // EDT, GMT-04:00
public static final TimeZone ZONE_EASTERN_STANDARD_TIME = TimeZone.getTimeZone("EST"); // GMT-05:00
public static final TimeZone ZONE_PACIFIC_DAYLIGHT_TIME = TimeZone.getTimeZone("America/Los_Angeles"); // PDT, GTM-0700
public static final TimeZone ZONE_PACIFIC_STANDARD_TIME = TimeZone.getTimeZone("Pacific/Pitcairn"); // PST, GMT-08:00

我又遇到了类似的问题“2009-11-01 08:44:44”。我尝试遍历所有 ID,但没有一个会打印 EDT。有趣的是“2010-11-01 08:44:44”会打印 EDT。从 1990 年到 2010 年,只有 2007 年、2008 年和 2010 年会打印 EDT。当我检查从 0 到 2499 的年份时,只有 431 年有 EDT,第一个是在 1940 年(这一定是引入 EDT 的时候)。所以在 560 年(1940-2499)中,只有 431 年的 ID 印有 EDT。从 1940 年到 2006 年,只有 10 年有打印 EDT 的 ID。2006年以后,每4年、5年、10年,就有一年不打印EDT。

于 2012-01-05T01:52:49.860 回答