Date comparision

Z

zzyzx

I wish to compare dates.

is 07/22/04 before 07/21/04

also in the longer format

07/22/2004 before 07/21/2004

any code or suggestions of libraries is appreciated.

Steve
 
C

Cid

I wish to compare dates.

is 07/22/04 before 07/21/04

also in the longer format

07/22/2004 before 07/21/2004

any code or suggestions of libraries is appreciated.

Steve

Here's an example using DateFormat for parsing and just using Date for
comparison (it implements Comparable).

I think Java really drops the ball on the Date type. It's a pain
having to deal with 3+ different classes to perform common operations
on the same logical entity (Date, Calendar, DateFormat). I know
internationalization can be tricky but this is just a mess.

Meanwhile, here's the code (didn't see a need for bringing Calendar
into it in this case).

import java.util.* ;
import java.text.* ;

public class test {
public static void main(String args[]) {
Date d1 ;
Date d2 ;

try {
DateFormat df = new SimpleDateFormat("MM/dd/yyyy") ;
d1 = df.parse(args[0]) ;
d2 = df.parse(args[1]) ;
}
catch ( Exception e ) {
System.out.println( "Bad date args: " + e.toString() ) ;
return ;
}

int cmp = d1.compareTo(d2) ;
System.out.println( "d1.compareTo(d2): " + d1.compareTo(d2) ) ;
}
}
 
J

Jacob

Cid said:
I think Java really drops the ball on the Date type. It's a pain
having to deal with 3+ different classes to perform common operations
on the same logical entity (Date, Calendar, DateFormat).

Agree. The problem is that java.util.Date is not a date
but a *time*. I appreciate time zone issues, daylight saving
time and ancient calendars, but in most applications this
complexities are not really needed.

I wrote a simple day class making a *date* front-end to
a calendar object, and including all common operations
(and no uncommon :)

See http://geosoft.no/software/day/Day.java.html
 
P

P.Hill

Cid said:
Meanwhile, here's the code (didn't see a need for bringing Calendar
into it in this case).

That's because the SimpleDateFormat brings it in for you; it does calculations
using a Calendar.

-Paul
 
P

P.Hill

Jacob said:
Cid wrote:

I wrote a simple day class making a *date* front-end to
a calendar object, and including all common operations
(and no uncommon :)

See http://geosoft.no/software/day/Day.java.html

It's too simple. It contains a bug!

I added the following code to the bottom of your main()
in order to show the number of days between April 1 of
this year and various other days in the same month.

Day oneDay = new Day( 2004, Calendar.APRIL, 1 );
for ( int i = 1; i < 6; i++ ) {
Day anotherDay = new Day( 2004, Calendar.APRIL, i );
System.out.println( "April " + i + " - April 1 = " +
oneDay.daysBetween(anotherDay));
}

Results in:

912
1037
7
April 1 - April 1 = 0
April 2 - April 1 = 1
April 3 - April 1 = 2
April 4 - April 1 = 2
April 5 - April 1 = 3

Both April 3 and April 4 being 2 days from April 1 and all later
days are off by one.
What's with that? Because in my local timezone April 4th is 23 hours long
since it is the 'Spring Forward' DLS date.

A quick fix is to add 1 hour before rounding, because I believe you
are always trying to work with 12 Midnight days. Another fix would be
to create (clone?) Calendars using an explicit GMT (with no DLS switching).

FWIW, the other test cases shown are not effected by this calculation since
the contain pairs of 23 and 25 hour days.

Daylight savings is such a pain!

HTH,
-Paul

p.s. copies of this message direct to the poster and for the record on the
newsgroup since the code from his site which others may have downloaded
contains a bug.
 
J

Jacob

P.Hill said:
It's too simple. It contains a bug!

I added the following code to the bottom of your main()
in order to show the number of days between April 1 of
this year and various other days in the same month.

Day oneDay = new Day( 2004, Calendar.APRIL, 1 );
for ( int i = 1; i < 6; i++ ) {
Day anotherDay = new Day( 2004, Calendar.APRIL, i );
System.out.println( "April " + i + " - April 1 = " +
oneDay.daysBetween(anotherDay));
}

Results in:

912
1037
7
April 1 - April 1 = 0
April 2 - April 1 = 1
April 3 - April 1 = 2
April 4 - April 1 = 2
April 5 - April 1 = 3

Both April 3 and April 4 being 2 days from April 1 and all later
days are off by one.
What's with that? Because in my local timezone April 4th is 23 hours long
since it is the 'Spring Forward' DLS date.

A quick fix is to add 1 hour before rounding, because I believe you
are always trying to work with 12 Midnight days. Another fix would be
to create (clone?) Calendars using an explicit GMT (with no DLS switching).

FWIW, the other test cases shown are not effected by this calculation since
the contain pairs of 23 and 25 hour days.

Daylight savings is such a pain!

Thanks.

I'll remove it from the site until it gets
properly fixed.
 
J

Jacob

P.Hill said:
A quick fix is to add 1 hour before rounding, because I believe you
are always trying to work with 12 Midnight days. Another fix would be
to create (clone?) Calendars using an explicit GMT (with no DLS switching).

Setting the back-end calendar timezone to GMT
fixed the problem. There might then be an
additional issue with the empty constructor
(making a Day of today), I'll fix that later.

Thanks again.
 
P

P.Hill

Jacob said:
Setting the back-end calendar timezone to GMT
fixed the problem. There might then be an
additional issue with the empty constructor
(making a Day of today), I'll fix that later.

No, I don't believe so, but a few unit test might be just the thing.

Each of your calls to calendar.get (Calendar.XXX), i.e. YEAR, MONTH and
DAY_OF_MONTH is asking the current calendar what it calls the current time.
It might a different answer than another calendar, but I think given any
calendar you want to know what it calls today then push that into the Day.

Also I might either try to do all constructors in terms of initialize( ... ) or
in terms of other contructors. I think in this case all calling
initialize would work well. This would mean changing Day( Calendar )
which calls another c'tor to use initialize( int year, int month, int day )
would make everything call initialize( ... ).

HTH,
-Paul
 
R

Roedy Green

Each of your calls to calendar.get (Calendar.XXX), i.e. YEAR, MONTH and
DAY_OF_MONTH is asking the current calendar what it calls the current time.
It might a different answer than another calendar, but I think given any
calendar you want to know what it calls today then push that into the Day.

have a look at the TestDate harness I used to ensure BigDate was
working correctly.

See http://mindprod.com/products.html#BIGDATE
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,734
Messages
2,569,441
Members
44,832
Latest member
GlennSmall

Latest Threads

Top