Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Confused about Date

Reply
Thread Tools

Confused about Date

 
 
Philipp
Guest
Posts: n/a
 
      10-23-2007
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,Da teFormat.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));
}

}
 
Reply With Quote
 
 
 
 
Hunter Gratzner
Guest
Posts: n/a
 
      10-23-2007
On Oct 23, 7:27 pm, Philipp <sicsic...@freesurf.ch> 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.

 
Reply With Quote
 
 
 
 
Philipp
Guest
Posts: n/a
 
      10-23-2007
Hunter Gratzner wrote:
> On Oct 23, 7:27 pm, Philipp <sicsic...@freesurf.ch> 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?

 
Reply With Quote
 
Eric Sosman
Guest
Posts: n/a
 
      10-23-2007
Philipp wrote On 10/23/07 16:28,:
> Hunter Gratzner wrote:
>
>>On Oct 23, 7:27 pm, Philipp <sicsic...@freesurf.ch> 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.

--

 
Reply With Quote
 
=?ISO-8859-1?Q?Arne_Vajh=F8j?=
Guest
Posts: n/a
 
      10-24-2007
Philipp wrote:
> Hunter Gratzner wrote:
>> On Oct 23, 7:27 pm, Philipp <sicsic...@freesurf.ch> 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.






 
Reply With Quote
 
Daniel Pitts
Guest
Posts: n/a
 
      10-24-2007
Arne Vajhøj wrote:
> Philipp wrote:
>> Hunter Gratzner wrote:
>>> On Oct 23, 7:27 pm, Philipp <sicsic...@freesurf.ch> 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/>
 
Reply With Quote
 
Philipp
Guest
Posts: n/a
 
      10-24-2007
Eric Sosman wrote:
> Philipp wrote On 10/23/07 16:28,:
>> Hunter Gratzner wrote:
>>
>>> On Oct 23, 7:27 pm, Philipp <sicsic...@freesurf.ch> 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.
 
Reply With Quote
 
Philipp
Guest
Posts: n/a
 
      10-24-2007
Arne Vajhøj wrote:
> Philipp wrote:
>> Hunter Gratzner wrote:
>>> On Oct 23, 7:27 pm, Philipp <sicsic...@freesurf.ch> 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

 
Reply With Quote
 
Gordon Beaton
Guest
Posts: n/a
 
      10-24-2007
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

--
 
Reply With Quote
 
Philipp
Guest
Posts: n/a
 
      10-24-2007
Philipp wrote:
> Arne Vajhøj wrote:
>> Philipp wrote:
>>> Hunter Gratzner wrote:
>>>> On Oct 23, 7:27 pm, Philipp <sicsic...@freesurf.ch> 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Date, date date date.... Peter Grison Java 10 05-30-2004 01:20 PM
Given a date, how to find the beginning date and ending date of that week Matt ASP General 11 11-08-2003 11:24 PM
Given a date, how to find the beginning date and ending date of that week Matt ASP .Net 1 11-08-2003 09:14 PM
Given a date, how to find the beginning date and ending date of that week Matt C Programming 3 11-08-2003 09:07 PM
Given a date, how to find the beginning date and ending date of that week Matt C++ 2 11-08-2003 08:30 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57