Java date rendering with a specific daylight mode

I have Java Date, which is from summer to summer. For example:

06/01/2009 06:00 PDT

My question is how to display this date as my local time zone and the current mode with summer savings (in my case, standard Pacific time)?

06/01/2009 05:00 AM PST

Java Date.toString()and SimpleDateFormatdisplays the date in original 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:

06/01/2009 06:00 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 get him to use a different daylight saving time mode?

:. , Windows XP. , 1 6:00, (, ..) 1 5 .

+3
4

, DateFormat TimeZone , . PST , PDT .

TimeZone, TimeZone.getAvailableIDs(), , PST : Pacific/Pitcairn SystemV/PST8. , , .

, GMT-8 . PST.

:. . TimeZone, TimeZone.getTimeZone(). Id 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)));

, .

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

:

    /*
     * 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 default format and time
     * zone. In other words, this will print differently if you set the
     * operating zone 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));
+1
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" ) Eastern Standard Time, , 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". , EDT. , "2010-11-01 08:44:44" EDT. 1990 2010 2007, 2008 2010 EDT. 0 2499, 431 EDT, 1940 ( , EDT ). 560 (1940-2499) 431 , EDT. 1940 2006 10 , , EDT. 2006 4, 5 10 , EDT.

0

Source: https://habr.com/ru/post/1723294/


All Articles