Confused about Date

P

Philipp

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));
}

}
 
H

Hunter Gratzner

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

Philipp

Hunter said:
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?
 
E

Eric Sosman

Philipp wrote On 10/23/07 16:28,:
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.
 
?

=?ISO-8859-1?Q?Arne_Vajh=F8j?=

Philipp said:
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.
 
D

Daniel Pitts

Arne said:
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.
 
P

Philipp

Eric said:
Philipp wrote On 10/23/07 16:28,:

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

Philipp

Arne said:
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
 
G

Gordon Beaton

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

--
 
P

Philipp

Philipp said:
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
 
E

Eric Sosman

Philipp said:
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.
 
L

Lew

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

Eric said:
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.
 
D

Dr J R Stockton

In comp.lang.java.programmer message said:
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.
 
P

Philipp

Lew said:
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
 
R

Roedy Green

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

Dr J R Stockton

In comp.lang.java.programmer message <h8msi3h08rl4cilu6ob6htqd6opj4l3j47
@4ax.com>, Sun, 4 Nov 2007 23:45:32, Roedy Green <[email protected]
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).
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,528
Members
45,000
Latest member
MurrayKeync

Latest Threads

Top