wood burning stoves
The moose likes JSF and the fly likes Custom Event Calendar Date Format Problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSF
Bookmark "Custom Event Calendar Date Format Problem" Watch "Custom Event Calendar Date Format Problem" New topic

Custom Event Calendar Date Format Problem

Scott Stephens

Joined: Jun 25, 2011
Posts: 23

I realize this isnt strictly a JSF Problem, but it is in a backing bean that is called by my JSF App.
My problem is that when I compare :

if (dt == result)

dt has the value 2012-11-02 and result has Fri Nov 02 20:29:23 CET 2012 even though I formatted it with SimpleDateFormat.
the static variable ferienTage is an array of holidays that is supposed to be valid for any year. The function compares the parameter dt to the array and returns
true/false if it is a holiday.
Why is there a difference. Both Date objects have been instansiated and initialized.

Maybe someone can help here.
Scott Stephens

Joined: Jun 25, 2011
Posts: 23

For anyone looking for an example how to compare dates, the following code now does it:
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 17410

The only thing more treacherous than dates when it comes to comparison are floating-point numbers. This is especially true when you read/write them from a database, where the granularity of the date/time objects doesn't correspond exactly to the granularity of the Java temporal objects.

A standard (not high-definition) java.util.Date or java.sql.Date is internally defined as the number of milliseconds since midnight Monday January 1, 1970. To determine whether two dates are the same day mathematically, extract the millisecond value from the date object as a long integer and slice off the fractional day part:

Or, if you prefer, don't multiply back and just count the actual days:

MS_PER_DAY is, of course 1000 (ms) * 60 (sec/minute) * 60 (min/hr) * 24 (hr/day), as a static long value. NOT an int!

In reality, there are somewhat arbitrary "leap seconds" so this isn't an astronomically precise way of doing things, but it's close enough for most business purposes.

An IDE is no substitute for an Intelligent Developer.
I agree. Here's the link: http://aspose.com/file-tools
subject: Custom Event Calendar Date Format Problem
It's not a secret anymore!