RE: Java date questions
RE: Java date questions
- Subject: RE: Java date questions
- From: "Pierre Bernard" <email@hidden>
- Date: Thu, 30 Oct 2003 17:36:38 +0100
- Thread-topic: Java date questions
Jonathan,
It is not deprecated, but I believe it once was and returned. It is not necessarily broken either. The Apple engineers and I just don't agree on what it should do. :-)
The two things we disagree on:
- To me what the documentation call 'preserving clock time' means what I want for _most_ uses: shifting a date by days, months or years brings me to a date that has the same time of day as the original one. To Apple it means that between both dates is a difference in hours of 24 times the number days. The time component may thus shift by one hour in either direction when crossing a DST switchover. Thus if you want to schedule something to happen every 24 hours, Apple's implentation is fine.
- Apple's implementation does all its computations in the GMT timezone. Based on the hypothesis that the relationship between GMT and whatever your local timezone is remains constant the result of a date computation should be the same no matter what timezone you perform it in. This might be correct if you consider the amount of time that actually passed. But again, for my uses this gives inappropriate results. Indeed as DST shift don't happen at the very same time in all timezones there are windows of time where such computations don't preserve clock time as the offset between time zones has changed.
Doing computations in GMT has another perverse sideeffect which seems just wrong. No matter how you put it, I can't understand the NSTimestamp behavior. Consider midnight on December 1st in my local timezone which is CET (GMT +1). Internally this is represented as a GMT timestamp of 11PM on November 30. Adding one month to that brings us to 11PM on December 30 GMT. Transformed back to CET for display this gives 0AM on December 31. One day off the expected result of 0AM January 1st.
Your call.
Pierre
--
Got a hang for movies?
http://homepage.mac.com/I_love_my/movies.html
-----Original Message-----
From: email@hidden [mailto:email@hidden]
Sent: Thursday, October 30, 2003 5:17 PM
To: Pierre Bernard
Cc: email@hidden
Subject: Re: Java date questions
Ooops, and here's Pierre suggesting that even though
NSTimestamp.timestampByAddingGregorianUnits _isn't_ deprecated (I thought it was, but it's not!), Pierre says it _ought_ to be deprecated, cause
it's broken, and you _should_ use GregorianCalendar for manipulation instead.
Although, implied by Pierre's code, is that the NSTimestamp 7-argument
constructor (which I thought was deprecated too, but also is NOT
deprecated) works fine.
Man, this stuff is so confusing. It's not really WO's fault; Java is
confusing enough when dealing with date/times already. But adding one
extra class (NSTimestamp) on to everything else doesn't help much.
--Jonathan
On Thu, 30 Oct 2003 10:35:39 +0100 "Pierre Bernard" wrote:
> Jonathan,
>
> Here's a handy utility method:
>
> /** Adds time to a date.<BR>
> * This utility method replaces NSTimestamp.
> * timestampByAddingGregorianUnits() which does all its computations
> in the
> * GMT timezone. This leads to several problems. For one DST changes
> don't
> * happen as expected. Adding 1 month to a CET midnight of a first of a
> * month does not bring us to the first of the next month, but only
> as many
> * days further as there are days in the preceeding month.
> *
> * @param date the original timestamp or date
> * @param year number of years to add
> * @param month number of months to add
> * @param day number of days to add
> * @param hour number of hours to add
> * @param minute number of minutes to add
> * @param second number of seconds to add
> *
> * @return a new date corresponding to original date to which we
> add specified time.
> **/
> public static NSTimestamp timestampByAddingGregorianUnits(
> Date date,
> int year,
> int month,
> int day,
> int hour,
> int minute,
> int second)
> {
> GregorianCalendar calendar = new GregorianCalendar();
>
> calendar.setTime(date);
>
> return new NSTimestamp(
> calendar.get(GregorianCalendar.YEAR) + year,
> calendar.get(GregorianCalendar.MONTH) + month + 1,
> calendar.get(GregorianCalendar.DAY_OF_MONTH) + day,
> calendar.get(GregorianCalendar.HOUR_OF_DAY) + hour,
> calendar.get(GregorianCalendar.MINUTE) + minute,
> calendar.get(GregorianCalendar.SECOND) + second,
> TimeZone.getDefault());
> }
>
> Pierre.
>
> --
> Got a hang for movies?
> http://homepage.mac.com/I_love_my/movies.html
>
> -----Original Message-----
> From: email@hidden [mailto:email@hidden]
> Sent: Thursday, October 30, 2003 12:58 AM
> To: email@hidden
> Subject: Java date questions
>
>
> [demime could not interpret encoding binary - treating as plain text]
> This isn't a WO question, it's more of a Java question, but I can't seem
> to figure it out anywhere.
>
> I have an NSTimestamp. I want to subtract seven days from it. If it's
> September 10, I want to end up with September 3. If it's September 1, I
> want to end up with October 25.
>
> I probably am gonna want to convert the NSTimestamp to a
> GregorianCalendar first. Fair enough. But I can't figure out what
> methods to call on the GregorianCalendar to do this date subtraction.
>
> --Jonathan
> _______________________________________________
> webobjects-dev mailing list | email@hidden
> Help/Unsubscribe/Archives:
> http://www.lists.apple.com/mailman/listinfo/webobjects-dev
> Do not post admin requests to the list. They will be ignored.
>
>
> **********************************************************************
> This email and any files transmitted with it are intended solely for
> the use of the individual or entity to whom they are addressed.
> If you have received this email in error please notify the sender
> of this message. (email@hidden)
> This email message has been checked for the presence of computer
> viruses; however this protection does not ensure this message is
> virus free.
> Banque centrale du Luxembourg; Tel ++352-4774-1; http://www.bcl.lu
> **********************************************************************
>
>
**********************************************************************
This email and any files transmitted with it are intended solely for
the use of the individual or entity to whom they are addressed.
If you have received this email in error please notify the sender
of this message. (email@hidden)
This email message has been checked for the presence of computer
viruses; however this protection does not ensure this message is
virus free.
Banque centrale du Luxembourg; Tel ++352-4774-1; http://www.bcl.lu
**********************************************************************
_______________________________________________
webobjects-dev mailing list | email@hidden
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/webobjects-dev
Do not post admin requests to the list. They will be ignored.