Re: Good and handy NSTimestamp utilites...
Re: Good and handy NSTimestamp utilites...
- Subject: Re: Good and handy NSTimestamp utilites...
- From: email@hidden
- Date: Mon, 3 Oct 2005 10:32:29 +0200
/**
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,
NSTimeZone.getDefault());
}
And don't even think about using NSTimestamp
for dates way in the past. It is BUGGY as hell !
And be careful not to mix NSTimeZone
with Java dates or a Java TimeZone with NSTimestamps.
I guess on my next project I will map
dates to a custom object which subclasses java.util.Date.
Pierre
webobjects-dev-bounces+pierre.bernard=email@hidden
wrote on 10/03/2005 06:42:25 AM:
> At 11:16 PM -0400 10/2/05, Nathan Walker wrote:
> >Is there a library I am missing out of? There's got to be the
> >ultimate date handling library out there...
>
> ... not NSTimeStamp, but useful is: http://joda-time.sourceforge.net/
> _______________________________________________
> Do not post admin requests to the list. They will be ignored.
> Webobjects-dev mailing list (email@hidden)
> Help/Unsubscribe/Update your Subscription:
>
> This email sent to email@hidden
>
**********************************************************************
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
**********************************************************************
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Webobjects-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden