Java Date, render with specific daylight mode

自闭症网瘾萝莉.ら 提交于 2019-12-06 01:09:59

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.

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