开发者

Java Date, render with specific daylight mode

开发者 https://www.devze.com 2022-12-12 01:47 出处:网络
I have a Java Date that is from this summer during daylight savings time. For example: Jun 01, 2009 06:00 AM PDT

I have a Java Date that is from this summer during daylight savings time. For example:

Jun 01, 2009 06:00 AM PDT

My question is, how do I display this date as my local time zone and current daylight savings mode (in my case Pacific Standard Time)?

Jun 01, 2009 05:00 AM PST

Java's Date.toString() and SimpleDateFormat displays the date in the original daylight savings mode. Example:

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

outputs:

Mon Jun 01 06:00: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)));

outputs:

Jun 01, 2009 06:00 AM PDT

What I really want to see开发者_开发百科 is 5:00 AM PST (which is equivalent to 6:00 AM PDT). Is there a way to force it to use another daylight savings mode?

Follow up: By the way, this is the Windows XP behavior. A file created on June 1, 6 AM will be seen (by Explorer, Command Prompt, etc) as June 1, 5 AM during the winter.


I don't think there's a way to force the DateFormat to apply a TimeZone format to a time that couldn't exist. PST is only applicable in the Winter months and PDT is only used in the summer.

I formatted this date with all of the TimeZones returned by TimeZone.getAvailableIDs(), and found two that returned me PST for that particular time: Pacific/Pitcairn and SystemV/PST8. You could try using one of those, but I have no idea what other rules apply to either of those timezone objects.

If you're just concerned with getting the time with that offset, you could use GMT-8 as your TimeZone. However, that string will show up in your formatted date string instead of PST.

UPDATE: Here's a way to take any timezone and have it ignore all daylight savings time rules. You can grab a TimeZone object then grab another TimeZone.getTimeZone() object. On the second object set the Id and rawOffset to the corresponding object from the first TimeZone.

For example:

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

This will print out exactly what you're looking for.


PST/PDT change throughout the year. Java attempts to determine which one is in affect on a given date. This things obviously change as the laws and regions change. Can you use an absolute time zone? Specifying the offset in GMT will be the same year around.

    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


this might do the trick:

    /*
     * 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));


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

Additional comments: zzz will print out PST, while z will print out GMT-08:00. TimeZone.getTimeZone("EST") works for for Eastern Standard Time, making me think this is a bug with TimeZone.getTimeZone("PST").

I suggest using predefined time zones, since they can be confusing:

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

I came across another time with a similar problem "2009-11-01 08:44:44". I tried iterating over all IDs and none would print EDT. The funny thing was that "2010-11-01 08:44:44" would print EDT. Between 1990 and 2010, only 2007, 2008 and 2010 would print EDT. When I checked the years from 0 to 2499, only 431 had EDT, with the first in 1940 (this must be when EDT was introduced). So of the 560 years (1940-2499), only 431 have an ID that prints EDT. From 1940 to 2006, there are only 10 years which have IDs which print EDT. After 2006, every 4, 5 or 10 years, there is a year which will not print EDT.

0

精彩评论

暂无评论...
验证码 换一张
取 消