Recipe 6.9 Comparing Dates


Problem

You need to compare two dates.

Solution

If the dates are in Date objects, compare with equals( ) and one of before( ) or after( ). If the dates are in long s, compare with == and either < or >.

Discussion

While Date implements equals( ) like any good class, it also provides before(Date) and after(Date), which compare one date with another to see which happened first. This can be used to determine the relationship among any two dates, as in Example 6-1.

Example 6-1. CompareDates.java
import java.util.*; import java.text.*; public class CompareDates {     public static void main(String[] args) throws ParseException {         DateFormat df = new SimpleDateFormat ("yyyy-MM-dd");         // Get Date 1         Date d1 = df.parse(args[0]);         // Get Date 2         Date d2 = df.parse(args[1]);         String relation;         if (d1.equals(d2))             relation = "the same date as";         else if (d1.before(d2))             relation = "before";         else             relation = "after";         System.out.println(d1 + " is " + relation + ' ' + d2);     } }

Running CompareDates with two close-together dates and the same date reveals that it seems to work:

> java CompareDates 2000-01-01 1999-12-31 Sat Jan 01 00:00:00 EST 2000 is after Fri Dec 31 00:00:00 EST 1999 > java CompareDates 2000-01-01 2000-01-01 Sat Jan 01 00:00:00 EST 2000 is the same date as Sat Jan 01 00:00:00 EST 2000

It would be interesting to see if DateFormat.parse( ) really does field rolling, as the documentation says. Apparently so!

> javaCompareDates 2001-02-29 2001-03-01  Thu Mar 01 00:00:00 EST 2001 is the same date as Thu Mar 01 00:00:00 EST 2001 >

Sometimes the API gives you a date as a long. For example, the File class has methods (detailed in Recipe 11.1) to give information such as when the last time a file on disk was modified. Example 6-2 shows a program similar to Example 6-1, but using the long value returned by the File's lastModified( ) method.

Example 6-2. CompareFileDates.java
import java.util.*; import java.io.File; public class CompareFileDates {     public static void main(String[] args) {         // Get the timestamp from file 1         String f1 = args[0];         long d1 = new File(f1).lastModified( );         // Get the timestamp from file 2         String f2 = args[1];         long d2 = new File(f2).lastModified( );         String relation;         if (d1 == d2)             relation = "the same age as";         else if (d1 < d2)             relation = "older than";         else             relation = "newer than";         System.out.println(f1 + " is " + relation + ' ' + f2);     } }

Running CompareFileDates on its source and class reveals that the class file is newer (that is, more up-to-date). Comparing a directory with itself gives the result of "the same age," as you'd expect:

> java CompareFileDates CompareFileDates.java CompareFileDates.class CompareFileDates.java is older thanCompareFileDates.class > java CompareFileDates  .  . . is the same age as .



Java Cookbook
Java Cookbook, Second Edition
ISBN: 0596007019
EAN: 2147483647
Year: 2003
Pages: 409
Authors: Ian F Darwin

Similar book on Amazon

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net