Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / November 2007

Tip: Looking for answers? Try searching our database.

Confused about Date

Thread view: 
Philipp - 23 Oct 2007 18:27 GMT
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));
  }

}
Hunter Gratzner - 23 Oct 2007 19:48 GMT
> 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.
Philipp - 23 Oct 2007 21:28 GMT
>> The ouput is not the same as the input. Why?
>>
[quoted text clipped - 3 lines]
>
> 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?
Eric Sosman - 23 Oct 2007 21:36 GMT
Philipp wrote On 10/23/07 16:28,:

>>>The ouput is not the same as the input. Why?
>>>
[quoted text clipped - 6 lines]
> 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.

Signature

Eric.Sosman@sun.com

Philipp - 24 Oct 2007 08:20 GMT
> Philipp wrote On 10/23/07 16:28,:
>>
[quoted text clipped - 11 lines]
> 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.
Eric Sosman - 24 Oct 2007 13:26 GMT
>> Philipp wrote On 10/23/07 16:28,:
>>>
[quoted text clipped - 22 lines]
>
> Not so instructive IMHO.

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

Signature

Eric Sosman
esosman@ieee-dot-org.invalid

Lew - 24 Oct 2007 14:19 GMT
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?

>     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.

Signature

Lew

Philipp - 24 Oct 2007 15:39 GMT
> Philipp wrote:
>>> It prints:
[quoted text clipped - 3 lines]
>
> 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
Arne Vajhøj - 24 Oct 2007 01:15 GMT
>>> The ouput is not the same as the input. Why?
>>>
[quoted text clipped - 6 lines]
> 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.
Daniel Pitts - 24 Oct 2007 04:55 GMT
>>>> The ouput is not the same as the input. Why?
>>>>
[quoted text clipped - 18 lines]
>
> PS: Unless time zone offset is zero.

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

Signature

Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/>

Philipp - 24 Oct 2007 08:25 GMT
>>>> The ouput is not the same as the input. Why?
>>>>
[quoted text clipped - 14 lines]
> !=
> (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
Gordon Beaton - 24 Oct 2007 08:29 GMT
> 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
[quoted text clipped - 4 lines]
> 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

--
Philipp - 24 Oct 2007 08:30 GMT
>>>>> The ouput is not the same as the input. Why?
>>>>>
[quoted text clipped - 25 lines]
> 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
Dr J R Stockton - 24 Oct 2007 14:26 GMT
>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.

Signature

(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.

Roedy Green - 04 Nov 2007 23:45 GMT
>    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).
Signature

Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com

Dr J R Stockton - 05 Nov 2007 20:11 GMT
In comp.lang.java.programmer message <h8msi3h08rl4cilu6ob6htqd6opj4l3j47
@4ax.com>, Sun, 4 Nov 2007 23:45:32, Roedy Green <see_website@mindprod.c
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).

Signature

(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.



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.