Simplest way to display time with correct daylight savings?

Discussion in 'Java' started by lordy, Jul 14, 2006.

  1. lordy

    lordy Guest

    I can create a SimpleDateFormatter with the correct timeZone.
    If but when I use format(Date D) it always displays the date
    using daylight savings (which are currently in force in the UK),
    (eg new Date(0) has 'HH' set to '01' when formatted.

    See example below:

    Do I need to do test each dates inDaylightTime() against the current system time's
    inDaylightTime() to work
    out how to display the time using daylight savings applicable at
    that time (rather than the current system time)



    ie doesnt the SimpleDateFormat.format() method have an option to do this?



    Example:


    import java.util.Date;
    import java.text.SimpleDateFormat;

    public class D {

    public static void main(String[] args) {

    SimpleDateFormat sdf = new SimpleDateFormat("yyy-MM-dd HH:mm");
    Date dt = new Date();

    //Should be London time - daylight savings
    System.out.println(sdf.getTimeZone());

    dt.setTime(0);
    System.out.println(sdf.format(dt));
    }
    }

    Output:
    sun.util.calendar.ZoneInfo[id="GB",offset=0,dstSavings=3600000,
    useDaylight=true,transitions=242,lastRule=java.util.SimpleTimeZone
    [id=GB,offset=0,dstSavings=3600000,useDaylight=true,startYear=0,
    startMode=2,startMonth=2,startDay=-1,startDayOfWeek=1,startTime=3600000,
    startTimeMode=2,endMode=2,endMonth=9,endDay=-1,endDayOfWeek=1,
    endTime=3600000,endTimeMode=2]]
    70-01-01 01:00 <<< Expected 70-01-01 00:00

    Lordy
     
    lordy, Jul 14, 2006
    #1
    1. Advertising

  2. lordy

    Guest

    lordy wrote:
    > I can create a SimpleDateFormatter with the correct timeZone.
    > If but when I use format(Date D) it always displays the date
    > using daylight savings (which are currently in force in the UK),
    > (eg new Date(0) has 'HH' set to '01' when formatted.
    >


    [snip]

    [snip]

    Your question seems to contradict your subject line: the subject line
    asks
    how to display the time with daylight savings time but the body of your
    post
    seems to be complaining that the daylight savings time is being shown.
    I'm
    really not clear on what you are after.

    Perhaps this code fragment will help:


    ================================================================TimeZone
    londonZone = TimeZone.getTimeZone("Europe/London");

    DateFormat dateFormatLondon
    =DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG,
    Locale.UK);

    dateFormatLondon.setTimeZone(londonZone);

    Calendar calendarLondon = Calendar.getInstance(londonZone);

    System.out.println("London Time (formatted): "
    +dateFormatLondon.format(calendarLondon.getTime()));
    ================================================================

    Explanation:

    The first line creates a TimeZone instance that reflects London
    England. The literal string "Europe/London" is one of hundreds of
    existing literals thatcould be used in the argument of getTimeZone()
    but please note that you can't put just _anything_ in that argument;
    I'm pretty sure "Europe/Swansea"would _not_ work as desired. To get a
    list of all of the valid literals,execute this loop:

    for (String id : TimeZone.getAvailableIDs()) {
    System.out.println(id);
    }

    Line 2 basically controls the appearance of the date as it is
    displayed. The first argument controls the way the date (year, month,
    and day) appears. The second argument controls the way the time (hour,
    minute, second) appears.The third argument essentially controls the
    language used in displaying the date and which separators are used
    between different parts of the date. Naturally, you have a variety of
    options for each of the three parameters so it's perfectly okay to set
    them to DateFormat.LONG, DateFormat.SHORT, Locale.US, respectively, if
    that's what you prefer.

    Line 3 tells the formatter which TimeZone to use.

    Line 4 gets you an instance of a Calendar using the specified TimeZone.

    Line 5 displays the date/time, formatted with the formatter created in
    Line 2.

    Therefore, if you want to show the time _without_ daylight savings
    time, you can accomplish it by using the UTC TimeZone ID like this:

    ===============================================================
    TimeZone utcZone = TimeZone.getTimeZone("UTC");

    DateFormat dateFormatUtc =
    DateFormat.getDateTimeInstance(DateFormat.LONG,DateFormat.LONG,
    Locale.UK);

    dateFormatUtc.setTimeZone(utcZone);

    Calendar calendarUtc = Calendar.getInstance(utcZone);

    System.out.println("UTC Time (formatted): "
    +dateFormatUtc.format(calendarUtc.getTime()));
    ================================================================

    If you execute both fragments of code in the order that I've given
    them, you get this for output:

    London Time (formatted): 14 July 2006 14:48:22 BST
    UTC Time (formatted): 14 July 2006 13:48:31 UTC

    As you can see, the London time takes daylight savings time into
    account and is an hour later while the UTC time ignores daylight
    savings time and is an hour later.


    I hope this helps you, whatever it is you are trying to do.

    --
    Rhino
     
    , Jul 14, 2006
    #2
    1. Advertising

  3. lordy

    lordy Guest

    On 2006-07-14, <> wrote:
    >
    > lordy wrote:
    >> I can create a SimpleDateFormatter with the correct timeZone.
    >> If but when I use format(Date D) it always displays the date
    >> using daylight savings (which are currently in force in the UK),
    >> (eg new Date(0) has 'HH' set to '01' when formatted.
    >>

    >
    > [snip]
    >
    > [snip]
    >
    > Your question seems to contradict your subject line: the subject line
    > asks
    > how to display the time with daylight savings time


    My subject says how to display with "Correct" daylight savings
    ie if a date is not in Daylight Savings then IMO the DateFormatter using local time
    should not apply DST offset. If the date is in daylight savings then the
    DateFormatter should apply DST offset.

    Att present it applies DST offset if the TineZone is inDaylight Savings
    ,
    ignoring whether or not the date to be displayed is in the DST period.

    Hence 0 = 1/1/1970 0:0 is in winter here and should not have DST
    applied. But a Simple DateFormatter created using LocalTime will display
    0 as 1/1/1970 1:00 which is the incorrect (for most purposes).

    ie why dies the DateFormatter check the TimeZone.inDaylightSavings(Date
    d) value and then decide if to display using daylight savings or not?

    Hope thats clearer..

    I'll look at your code now :)

    It could be a difference between "GB" and "London" TimeZones ???

    Lordy
     
    lordy, Jul 14, 2006
    #3
  4. lordy

    lordy Guest

    On 2006-07-14, <> wrote:
    >
    > lordy wrote:
    >> I can create a SimpleDateFormatter with the correct timeZone.
    >> If but when I use format(Date D) it always displays the date
    >> using daylight savings (which are currently in force in the UK),
    >> (eg new Date(0) has 'HH' set to '01' when formatted.
    >>

    >
    > [snip]
    >
    > [snip]
    >
    > Your question seems to contradict your subject line: the subject line
    > asks
    > how to display the time with daylight savings time but the body of your
    > post
    > seems to be complaining that the daylight savings time is being shown.
    > I'm
    > really not clear on what you are after.


    Sorted. My issue was that it was displaying Date(0) as 1/1/1970 01:00
    But in the UK the summer time act was passed in 1972 ! Dates after 1972
    are as I expected.

    Eg run this little program and look at the hours column:
    ============================
    import java.util.Date;
    import java.util.TimeZone;
    import java.text.SimpleDateFormat;

    public class D {

    public static void main(String[] args) {

    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm zzz");
    sdf.setTimeZone(TimeZone.getTimeZone("GB"));

    long secondsInYear = 365*24*60*60*1000L ; //Ignore leap years etc.
    long now = System.currentTimeMillis();

    Date dt = new Date();

    for(long l = 0 ; l < now ; l += secondsInYear ) {
    dt.setTime(l);
    System.out.println(l + " = " + sdf.format(dt));
    }
    }
    }
    =============================


    Still odd that 0L is displayed as 1:00 GMT but there is probably some
    history behind it.

    Lordy
     
    lordy, Jul 14, 2006
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ryan Ternier
    Replies:
    1
    Views:
    1,472
    Ryan Ternier
    Oct 14, 2005
  2. js
    Replies:
    1
    Views:
    791
  3. Polaris431
    Replies:
    4
    Views:
    488
    =?ISO-8859-1?Q?G=F6ran_Andersson?=
    May 28, 2007
  4. Replies:
    2
    Views:
    148
    Dr John Stockton
    Jun 25, 2005
  5. Washington Ratso
    Replies:
    1
    Views:
    63
    Chris “Kwpolska†Warrick
    Apr 3, 2014
Loading...

Share This Page