2009-11-19 105 views
3

我有一个在夏令时期间从今年夏天开始的Java Date。例如:Java日期,使用特定的日光模式进行渲染

2009年6月1日上午06点PDT

我的问题是,我怎么显示这个日期作为我的本地时区和当前夏令模式(在我的情况太平洋标准时间)?

2009年6月1日上午05点PST

Java的Date.toString()SimpleDateFormat显示在夏令模式的日期。例如:

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

输出:

周一6月1日6时〇〇分00秒PDT 2009

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 AM PDT

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

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

+9

这是强制性的,每当有人提到关于Java中的日期以提及乔达时间的问题时。我不知道它是否会做你想要的,但它是强制性的。 – 2009-11-19 01:36:23

+1

我认为有人没有直接的头。谁在问这个?这个不成立。日期/时间在DSMode中是固定的,并且仅随TZ(地理位移)而变化。 – Don 2009-11-19 01:59:59

回答

2

我不认为有一种方法可以强制DateFormatTimeZone格式应用于无法存在的时间。 PST仅适用于冬季,PDT仅适用于夏季。

我把这个日期格式化了,所有的TimeZone都返回了TimeZone.getAvailableIDs(),发现有两个让我在那个特定时间返回PST:太平洋/皮特凯恩和SystemV/PST8。你可以尝试使用其中的一种,但我不知道其他规则适用于那些时区对象。

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

更新:这是采取任何时区并忽略所有夏令时规则的方法。您可以抓住一个TimeZone对象,然后抓住另一个TimeZone.getTimeZone()对象。在第二个对象上,将IdrawOffset设置为从第一个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))); 

这将打印出你在寻找什么。

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)); 
+2

-1当日期确实在PST时,冬季的日期将会不正确。 – 2009-11-19 03:07:12

+0

谢谢,杰森,我明白这是如何误导,我会更新我的答案,以便从日期减去一小时后更清楚我想表现的内容。 – Upgradingdave 2009-11-19 11:01:57

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 
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,而ž会打印出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 8时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。