Re: Recording and looking up dates, when day-wide precision is required
Re: Recording and looking up dates, when day-wide precision is required
- Subject: Re: Recording and looking up dates, when day-wide precision is required
- From: Florijan Stamenkovic <email@hidden>
- Date: Tue, 23 Dec 2008 14:56:58 -0400
Uhm, some more thoughts... *Please* help me make sense out of this...
Setting a formatter to GMT will for any given date String produce
this: <date> 00:00:00 GMT. However, format the resulting Date with a
formatter that has GMT-0400, this results in: <date - 1> 21:00:00
-0400. Which means, if a user is in GMT-0400, and types in "Dec 21,
2008" and I format it using the formatter set to GMT, then the actual
java.util.Date produced, when formatted with a format that is set to
GMT-0400 will get "Dec 20, 2008". Well, perhaps my numbers a shi*,
but I think the principle of what I'm saying is valid.
What we need is a date the user provides, set in GMT, at 11:30. That
is because timezones go from GMT-1100 to GMT+1200, so the only times
that produce textually the same date (viewed in whichever timezone)
are those in between of 11:00 GMT and 12:00 GMT, endpoints excluded.
In short:
Dec 21, 2008 11:30 GMT should render as "Dec 21" in all time zones.
However add or subtract an hour, and that's not true anymore, it will
render as a different date, in one time zone.
It seems to me that there are two solutions.
1. Parse using GMT, and then add 11 hours and 30 minutes to the
produced java.util.Date.
2. Parse in the user's time zone, and normalize using the method
provided below.
Am I correct?
Well, some additional reading suggests that "the earliest and latest
time zones are 26 hours apart", which is something that simply does
not make sense to me (and I don't really want it to), so let me
rephrase the question: am I sufficiently correct to maintain the
reliability of the software I'm working on?
F
the code I made for this (included in org.jbnd.support.JBNDUtil)
// used in the normalize(...) method
private static final Calendar GMTCalendar =
Calendar.getInstance(TimeZone.getTimeZone("GMT"));
/**
* Takes a <tt>Date</tt> and a <tt>TimeZone</tt>, and returns a
normalized
* version of it. The returned <tt>Date</tt> that has the time of
11:30:00
* GMT. It's year, month and day are same as the given <tt>original</
tt> has
* in the given <tt>timezone</tt>. If <tt>timezone</tt> is <tt>null</
tt>,
* the local timezone is assumed to apply.
* <p>
* The returned <tt>Date</tt> will, when formatted, have the same year,
* month, and day as the original date has in it's timezone,
regardless of
* the timezone it is represented in.
*
* @param original The date to normalize.
* @param timezone The timezone in which the <tt>original</tt> is
in, if
* <tt>null</tt>, the local timezone is assumed to apply.
* @return See above.
*/
public static Date normalize(Date original, TimeZone timezone){
// first, get a calendar for the original timezone
Calendar c = Calendar.getInstance(
timezone == null ? TimeZone.getDefault() : timezone);
c.setTime(original);
// make a date using the GMT calendar
GMTCalendar.set(
c.get(Calendar.YEAR),
c.get(Calendar.MONTH),
c.get(Calendar.DATE),
11, 30, 0);
return GMTCalendar.getTime();
}
_______________________________________________
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