RE: Java date questions
RE: Java date questions
- Subject: RE: Java date questions
- From: "Pierre Bernard" <email@hidden>
- Date: Thu, 30 Oct 2003 10:35:39 +0100
- Thread-topic: Java date questions
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.