Confused about Date

Discussion in 'Java' started by Philipp, Oct 23, 2007.

  1. Philipp

    Philipp Guest

    Hello,

    I'm a bit confused about Date. In the example below, I get the present
    Date, format it to Strings using DateFormaters, reparse the String using
    the _same_ DateFormater and recombine them by adding.

    The ouput is not the same as the input. Why?

    In my locale it outputs:
    Before: 23.10.07 19:16:49
    After: 23.10.07 18:16:49

    Thanks Phil


    -- SSCCE --

    import java.text.DateFormat;
    import java.text.ParseException;
    import java.util.Date;

    public class TimeTest {
    public static void main(String[] args) {
    DateFormat df = DateFormat.getDateInstance(DateFormat.SHORT);
    DateFormat tf = DateFormat.getTimeInstance(DateFormat.MEDIUM);
    DateFormat dtf =
    DateFormat.getDateTimeInstance(DateFormat.SHORT,DateFormat.MEDIUM);

    // getting actual time
    Date now = new Date();

    System.out.println("Before: "+ dtf.format(now));

    // separating in date and time strings
    String dateString = df.format(now);
    String timeString = tf.format(now);

    Date date = null;
    Date time = null;
    try {
    // parsing date and time strings
    date = df.parse(dateString);
    time = tf.parse(timeString);
    } catch (ParseException e) {
    e.printStackTrace();
    }

    // recombining date and time
    Date after = new Date(date.getTime() + time.getTime());

    System.out.println("After: "+ dtf.format(after));
    }

    }
     
    Philipp, Oct 23, 2007
    #1
    1. Advertisements

  2. On Oct 23, 7:27 pm, Philipp <> wrote:
    > The ouput is not the same as the input. Why?
    >
    > In my locale it outputs:
    > Before: 23.10.07 19:16:49
    > After: 23.10.07 18:16:49


    You forgot about time zones.
     
    Hunter Gratzner, Oct 23, 2007
    #2
    1. Advertisements

  3. Philipp

    Philipp Guest

    Hunter Gratzner wrote:
    > On Oct 23, 7:27 pm, Philipp <> wrote:
    >> The ouput is not the same as the input. Why?
    >>
    >> In my locale it outputs:
    >> Before: 23.10.07 19:16:49
    >> After: 23.10.07 18:16:49

    >
    > You forgot about time zones.
    >


    Yes, but why is the Date to String conversion time-zone insensitive
    while the String to Date is time-zone sensitive?
     
    Philipp, Oct 23, 2007
    #3
  4. Philipp

    Eric Sosman Guest

    Philipp wrote On 10/23/07 16:28,:
    > Hunter Gratzner wrote:
    >
    >>On Oct 23, 7:27 pm, Philipp <> wrote:
    >>
    >>>The ouput is not the same as the input. Why?
    >>>
    >>>In my locale it outputs:
    >>>Before: 23.10.07 19:16:49
    >>>After: 23.10.07 18:16:49

    >>
    >>You forgot about time zones.
    >>

    >
    >
    > Yes, but why is the Date to String conversion time-zone insensitive
    > while the String to Date is time-zone sensitive?


    It would be instructive to print the two Dates
    that you parse from the text strings, before adding
    their times together.

    --
     
    Eric Sosman, Oct 23, 2007
    #4
  5. Philipp wrote:
    > Hunter Gratzner wrote:
    >> On Oct 23, 7:27 pm, Philipp <> wrote:
    >>> The ouput is not the same as the input. Why?
    >>>
    >>> In my locale it outputs:
    >>> Before: 23.10.07 19:16:49
    >>> After: 23.10.07 18:16:49

    >>
    >> You forgot about time zones.

    >
    > Yes, but why is the Date to String conversion time-zone insensitive
    > while the String to Date is time-zone sensitive?


    Both are time zone sensitive.

    Which is exactly why you can not add as you do.

    (local date - timezone offset) + (local time - timezone offset)
    !=
    (local date + local time - timezone offset)

    Arne

    PS: Unless time zone offset is zero.
     
    =?ISO-8859-1?Q?Arne_Vajh=F8j?=, Oct 24, 2007
    #5
  6. Philipp

    Daniel Pitts Guest

    Arne Vajhøj wrote:
    > Philipp wrote:
    >> Hunter Gratzner wrote:
    >>> On Oct 23, 7:27 pm, Philipp <> wrote:
    >>>> The ouput is not the same as the input. Why?
    >>>>
    >>>> In my locale it outputs:
    >>>> Before: 23.10.07 19:16:49
    >>>> After: 23.10.07 18:16:49
    >>>
    >>> You forgot about time zones.

    >>
    >> Yes, but why is the Date to String conversion time-zone insensitive
    >> while the String to Date is time-zone sensitive?

    >
    > Both are time zone sensitive.
    >
    > Which is exactly why you can not add as you do.
    >
    > (local date - timezone offset) + (local time - timezone offset)
    > !=
    > (local date + local time - timezone offset)
    >
    > Arne
    >
    > PS: Unless time zone offset is zero.


    Right, you should probably use the Calendar class to handle date
    manipulation in such a way.


    --
    Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/>
     
    Daniel Pitts, Oct 24, 2007
    #6
  7. Philipp

    Philipp Guest

    Eric Sosman wrote:
    > Philipp wrote On 10/23/07 16:28,:
    >> Hunter Gratzner wrote:
    >>
    >>> On Oct 23, 7:27 pm, Philipp <> wrote:
    >>>
    >>>> The ouput is not the same as the input. Why?
    >>>>
    >>>> In my locale it outputs:
    >>>> Before: 23.10.07 19:16:49
    >>>> After: 23.10.07 18:16:49
    >>> You forgot about time zones.
    >>>

    >>
    >> Yes, but why is the Date to String conversion time-zone insensitive
    >> while the String to Date is time-zone sensitive?

    >
    > It would be instructive to print the two Dates
    > that you parse from the text strings, before adding
    > their times together.
    >


    It prints:

    Before: 24.10.07 09:03:26
    String date: 24.10.07
    String time: 09:03:26
    Parsed date: 24.10.07 00:00:00
    Parsed time: 01.01.70 09:03:26
    Added: 24.10.07 08:03:26

    Not so instructive IMHO.
     
    Philipp, Oct 24, 2007
    #7
  8. Philipp

    Philipp Guest

    Arne Vajhøj wrote:
    > Philipp wrote:
    >> Hunter Gratzner wrote:
    >>> On Oct 23, 7:27 pm, Philipp <> wrote:
    >>>> The ouput is not the same as the input. Why?
    >>>>
    >>>> In my locale it outputs:
    >>>> Before: 23.10.07 19:16:49
    >>>> After: 23.10.07 18:16:49
    >>>
    >>> You forgot about time zones.

    >>
    >> Yes, but why is the Date to String conversion time-zone insensitive
    >> while the String to Date is time-zone sensitive?

    >
    > Both are time zone sensitive.
    >
    > Which is exactly why you can not add as you do.
    >
    > (local date - timezone offset) + (local time - timezone offset)
    > !=
    > (local date + local time - timezone offset)


    Thank you for this clarification! This definitely makes sense.

    Although, I'm still not clear how to cleanly solve my problem:
    My user inputs a date in one field and a time in another field. I want
    to recombine them to one Date object. I can parse each into a Date, but
    how should I recombine them? The Calendar class (as suggested by D.
    Pitts) does not contain Date arithmetics.

    Do I have to break my Date objects into DD.MM.YY HH.MM.SS etc and
    reconstruct a Calendar object from this?

    Phil
     
    Philipp, Oct 24, 2007
    #8
  9. On Wed, 24 Oct 2007 09:25:50 +0200, Philipp wrote:
    > Although, I'm still not clear how to cleanly solve my problem:
    > My user inputs a date in one field and a time in another field. I want
    > to recombine them to one Date object. I can parse each into a Date, but
    > how should I recombine them? The Calendar class (as suggested by D.
    > Pitts) does not contain Date arithmetics.
    >
    > Do I have to break my Date objects into DD.MM.YY HH.MM.SS etc and
    > reconstruct a Calendar object from this?


    Set the timezone in your DateFormats to UTC.

    /gordon

    --
     
    Gordon Beaton, Oct 24, 2007
    #9
  10. Philipp

    Philipp Guest

    Philipp wrote:
    > Arne Vajhøj wrote:
    >> Philipp wrote:
    >>> Hunter Gratzner wrote:
    >>>> On Oct 23, 7:27 pm, Philipp <> wrote:
    >>>>> The ouput is not the same as the input. Why?
    >>>>>
    >>>>> In my locale it outputs:
    >>>>> Before: 23.10.07 19:16:49
    >>>>> After: 23.10.07 18:16:49
    >>>>
    >>>> You forgot about time zones.
    >>>
    >>> Yes, but why is the Date to String conversion time-zone insensitive
    >>> while the String to Date is time-zone sensitive?

    >>
    >> Both are time zone sensitive.
    >>
    >> Which is exactly why you can not add as you do.
    >>
    >> (local date - timezone offset) + (local time - timezone offset)
    >> !=
    >> (local date + local time - timezone offset)

    >
    > Thank you for this clarification! This definitely makes sense.
    >
    > Although, I'm still not clear how to cleanly solve my problem:
    > My user inputs a date in one field and a time in another field. I want
    > to recombine them to one Date object. I can parse each into a Date, but
    > how should I recombine them? The Calendar class (as suggested by D.
    > Pitts) does not contain Date arithmetics.
    >
    > Do I have to break my Date objects into DD.MM.YY HH.MM.SS etc and
    > reconstruct a Calendar object from this?


    OK sorry about the noise, got my solution.
    I can combine both Strings and parse them with one formatter.

    Thank you everybody for your help.
    Phil
     
    Philipp, Oct 24, 2007
    #10
  11. Philipp

    Eric Sosman Guest

    Philipp wrote:
    > Eric Sosman wrote:
    >> Philipp wrote On 10/23/07 16:28,:
    >>> Hunter Gratzner wrote:
    >>>
    >>>> On Oct 23, 7:27 pm, Philipp <> wrote:
    >>>>
    >>>>> The ouput is not the same as the input. Why?
    >>>>>
    >>>>> In my locale it outputs:
    >>>>> Before: 23.10.07 19:16:49
    >>>>> After: 23.10.07 18:16:49
    >>>> You forgot about time zones.
    >>>>
    >>>
    >>> Yes, but why is the Date to String conversion time-zone insensitive
    >>> while the String to Date is time-zone sensitive?

    >>
    >> It would be instructive to print the two Dates
    >> that you parse from the text strings, before adding
    >> their times together.
    >>

    >
    > It prints:
    >
    > Before: 24.10.07 09:03:26
    > String date: 24.10.07
    > String time: 09:03:26
    > Parsed date: 24.10.07 00:00:00
    > Parsed time: 01.01.70 09:03:26
    > Added: 24.10.07 08:03:26
    >
    > Not so instructive IMHO.


    Look more closely at the "Parsed time" line. Also, see
    Arne Vajhøj's response.

    --
    Eric Sosman
    lid
     
    Eric Sosman, Oct 24, 2007
    #11
  12. Philipp

    Lew Guest

    Philipp wrote:
    >> It prints:
    >> Parsed time: 01.01.70 09:03:26
    >>
    >> Not so instructive IMHO.


    How can you say that? It is completely instructive. And why are you using
    two-digit year representation?

    Eric Sosman wrote:
    > Look more closely at the "Parsed time" line. Also, see
    > Arne Vajhøj's response.


    Those were turbulent times. The /Beatles/ made their last studio appearance
    the very next day, did you know that? I hadn't. Biafra was big in the news.
    Diana Ross left the /Supremes/ not long after that day.

    That particular day was a Thursday.

    --
    Lew
     
    Lew, Oct 24, 2007
    #12
  13. In comp.lang.java.programmer message <>,
    Tue, 23 Oct 2007 19:27:40, Philipp <> posted:
    >
    >In my locale it outputs:
    >Before: 23.10.07 19:16:49
    >After: 23.10.07 18:16:49
    >


    In such cases it would be helpful to state what the current offset from
    GMT/UTC was in your locale - which might not be the same as that of your
    News service. You could add, in case it matters (which it should not)
    the locale state of Summer Time.

    Time Zone as such (properly called) should be irrelevant.

    --
    (c) John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v6.05 IE 6.
    Web <URL:http://www.merlyn.demon.co.uk/> - w. FAQish topics, links, acronyms
    PAS EXE etc : <URL:http://www.merlyn.demon.co.uk/programs/> - see 00index.htm
    Dates - miscdate.htm moredate.htm js-dates.htm pas-time.htm critdate.htm etc.
     
    Dr J R Stockton, Oct 24, 2007
    #13
  14. Philipp

    Philipp Guest

    Lew wrote:
    > Philipp wrote:
    >>> It prints:
    >>> Parsed time: 01.01.70 09:03:26
    >>>
    >>> Not so instructive IMHO.

    >
    > How can you say that? It is completely instructive.


    Yes and No. I do see (and expected) that when I parse a time (without a
    date), it is parsed as if it is that time past midnight of 1.1.1970.

    My error (as explained to me by others in this thread) is not in the
    parsing, which occurs correctly it seems, but in the addition of two
    Date objects. The by adding two Date objects you actually add the
    TimeZone twice.

    I thought that
    24.10.07 00:00:00 + 01.01.70 09:03:26 would result in

    24.10.07 09:03:26

    > And why are you
    > using two-digit year representation?


    That's the way DateFormat formats with a precision "DateFormat.SHORT" in
    my Locale.

    > Those were turbulent times. The /Beatles/ made their last studio
    > appearance the very next day, did you know that? I hadn't. Biafra was
    > big in the news. Diana Ross left the /Supremes/ not long after that day.
    >
    > That particular day was a Thursday.
    >


    And I had a few more years to wait to see the light of day... :)

    Phil
     
    Philipp, Oct 24, 2007
    #14
  15. Philipp

    Roedy Green Guest

    On Tue, 23 Oct 2007 19:27:40 +0200, Philipp <>
    wrote, quoted or indirectly quoted someone who said :

    > Date time = null;
    > try {
    > // parsing date and time strings
    > date = df.parse(dateString);
    > time = tf.parse(timeString);


    I think the essence of your problem is that Java Dates are neither
    dates nor times. They are TIMESTAMPS -- a combined date/time
    milliseconds since 1970-01-01 0:00

    If you want pure dates, use BigDate. See
    http://mindprod.com/jgloss/bigdate.html
    if you want pure time, use int, (minutes or seconds).
    --
    Roedy Green Canadian Mind Products
    The Java Glossary
    http://mindprod.com
     
    Roedy Green, Nov 4, 2007
    #15
  16. In comp.lang.java.programmer message <h8msi3h08rl4cilu6ob6htqd6opj4l3j47
    @4ax.com>, Sun, 4 Nov 2007 23:45:32, Roedy Green <
    om.invalid> posted:
    >
    >I think the essence of your problem is that Java Dates are neither
    >dates nor times. They are TIMESTAMPS -- a combined date/time
    >milliseconds since 1970-01-01 0:00
    >


    Since 1970-01-01 00:00:00.000 UTC, to be exact (noting that it's some
    sort of proleptic UTC).

    --
    (c) John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v6.05 IE 6.
    Web <URL:http://www.merlyn.demon.co.uk/> - w. FAQish topics, links, acronyms
    PAS EXE etc : <URL:http://www.merlyn.demon.co.uk/programs/> - see 00index.htm
    Dates - miscdate.htm moredate.htm js-dates.htm pas-time.htm critdate.htm etc.
     
    Dr J R Stockton, Nov 5, 2007
    #16
    1. Advertisements

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. Matt
    Replies:
    1
    Views:
    750
    Matthew Speed
    Nov 8, 2003
  2. Peter Grison

    Date, date date date....

    Peter Grison, May 28, 2004, in forum: Java
    Replies:
    10
    Views:
    3,582
    Michael Borgwardt
    May 30, 2004
  3. Matt
    Replies:
    2
    Views:
    681
    Pete Becker
    Nov 8, 2003
  4. Matt
    Replies:
    3
    Views:
    824
    Richard Heathfield
    Nov 8, 2003
  5. Matt
    Replies:
    11
    Views:
    526
    Aaron Bertrand [MVP]
    Nov 8, 2003
Loading...

Share This Page