Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Need to Compare to dates
Message
De
20/10/2001 00:34:00
 
Information générale
Forum:
Java
Catégorie:
Autre
Divers
Thread ID:
00570781
Message ID:
00571260
Vues:
17
>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() );
    }
  }
}
Précédent
Répondre
Fil
Voir

Click here to load this message in the networking platform