Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Need to Compare to dates
Message
From
20/10/2001 00:34:00
 
General information
Forum:
Java
Category:
Other
Miscellaneous
Thread ID:
00570781
Message ID:
00571260
Views:
19
>Can someone show me the Java code needed to compare to dates that the user enters MM/DD/YYYY and ensure that one is not greater than another. So I have two date fields on a form and want to ensure that the second date is not greater than the first date and display a message to the user that it is an error.
>
>TIA

What class is the date stored as?

Assuming String class:
package DateCompare;

import java.text.*;
import java.io.*;
import java.util.Date;

public class DateCompare {

  public DateCompare() {
  }

  public static void main( String[] args ) {
    DateCompare dateCompare = new DateCompare();

    SimpleDateFormat sdf = new SimpleDateFormat( "m/d/y" );

    try {
      Date d1 = sdf.parse( "01/01/2001" );
      Date d2 = sdf.parse( "05/05/2001" );
      System.out.println( d1.compareTo( d2 ) );
    }
    catch ( ParseException e ) {
      System.out.println( e.getMessage() );
    }
    System.out.println( "Hit ENTER to continue..." );
    try {
      System.in.read(); // wait for input
    }
    catch ( IOException e ) {
      System.out.println( e.getMessage() );
    }
  }
}
Previous
Reply
Map
View

Click here to load this message in the networking platform