Created
July 3, 2013 05:46
-
-
Save yelinaung/5915722 to your computer and use it in GitHub Desktop.
Java Date Compare
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package introtocs; | |
import java.util.Date; | |
import java.text.ParseException; | |
import java.text.SimpleDateFormat; | |
public class DateCompare { | |
/** | |
* @param args | |
* @throws ParseException | |
*/ | |
public static void main(String[] args) throws ParseException { | |
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); | |
Date date1 = sdf.parse("2009-12-31"); | |
Date date2 = sdf.parse("2010-01-31"); | |
// Check if date 1 is before date 2 | |
if (date1.before(date2)) { | |
System.out.println("Its before"); | |
} | |
// Check if date 1 is after date 2 | |
if (date1.after(date2)) { | |
System.out.println("It's after."); | |
} | |
// Check if date 1 = date 2 | |
if (date1.compareTo(date2) == 0) { | |
System.out.println("It's equal"); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment