Re: Java date questions
Re: Java date questions
- Subject: Re: Java date questions
- From: email@hidden
- Date: Thu, 30 Oct 2003 11:17:20 CDT
- Priority: 3 (Normal)
[demime could not interpret encoding binary - treating as plain text]
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
> **********************************************************************
_______________________________________________
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.