How to compare Time and Date in Java.
- In Java8, LocalDateTime, LocalDate, LocalTime, ZonedDateTime are provided.
You can compare date/time in this API.
- You can compare date/time with Date and Calendar used before Java8.
Java 8: Date/Time API Comparison
LocalDateTime, LocalDate, LocalTime, ZonedDateTime provide the following methods:
- isBefore() : Returns true if it is past than the argument
- isAfter() : Returns true if it is more future than the argument
- isEqual() : Returns true when the time is the same as the argument
public void compareDate1() throws ParseException {
LocalDateTime date1 = LocalDateTime.parse("2019-07-12T10:11:50.000");
LocalDateTime date2 = LocalDateTime.parse("2019-11-12T15:20:31.000");
if (date1.isBefore(date2)) {
System.out.println("Date1 is before Date2");
}
if (date1.isAfter(date2)) {
System.out.println("Date1 is after Date2");
}
if (date1.isEqual(date2)) {
System.out.println("Date1 is equal Date2");
}
}
result
2019-07-12T10:11:50
2019-11-12T15:20:31
Date1 is before Date2
Compare Dates by Date
The Date class provides the following methods:
- before() : Returns true if it is past than the argument
- after() : Returns true if it is more future than the argument
- equal() : Returns true when the time is the same as the argument
public void compareDate2() throws ParseException {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date date1 = sdf.parse("2019-07-12");
Date date2 = sdf.parse("2019-11-01");
System.out.println(sdf.format(date1));
System.out.println(sdf.format(date2));
if(date1.after(date2)){
System.out.println("Date1 is after Date2");
}
if(date1.before(date2)){
System.out.println("Date1 is before Date2");
}
if(date1.equals(date2)){
System.out.println("Date1 is equal Date2");
}
}
result
2019-07-12
2019-11-01
Date1 is before Date2
Compare Dates from Date (compareTo)
The Date class provides a compareTo() method, which allows you to compare dates.
- compareTo() > 0 : future than argument
- compareTo() < 0 : past the argument
- compareTo() == 0 : time equal to argument
public void compareDate3() throws ParseException {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date date1 = sdf.parse("2019-07-12");
Date date2 = sdf.parse("2019-11-01");
System.out.println(sdf.format(date1));
System.out.println(sdf.format(date2));
if (date1.compareTo(date2) > 0) {
System.out.println("Date1 is after Date2");
} else if(date1.compareTo(date2) < 0){
System.out.println("Date1 is before Date2");
} else {
System.out.println("Date1 is equal to Date2");
}
}
result
2019-07-12
2019-11-01
Date1 is before Date2
Compare Dates in Calendar
The Calendar class provides the following methods:
- before() : Returns true if it is past than the argument
- after() : Returns true if it is more future than the argument
- equal() : Returns true when the time is the same as the argument
public void compareDate4() throws ParseException {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date date1 = sdf.parse("2019-07-12");
Date date2 = sdf.parse("2019-11-01");
System.out.println(sdf.format(date1));
System.out.println(sdf.format(date2));
Calendar cal1 = Calendar.getInstance();
Calendar cal2 = Calendar.getInstance();
cal1.setTime(date1);
cal2.setTime(date2);
if(cal1.after(cal2)){
System.out.println("Date1 is after Date2");
}
if(cal1.before(cal2)){
System.out.println("Date1 is before Date2");
}
if(cal1.equals(cal2)){
System.out.println("Date1 is equal Date2");
}
}
result
2019-07-12
2019-11-01
Date1 is before Date2
Reference
Related Posts
- Java - Remove items from List while iterating
- Java - How to find key by value in HashMap
- Java - Update the value of a key in HashMap
- Java - How to put quotes in a string
- Java - How to put a comma (,) after every 3 digits
- BiConsumer example in Java 8
- Java 8 - Consumer example
- Java 8 - BinaryOperator example
- Java 8 - BiPredicate Example
- Java 8 - Predicate example
- Java 8 - Convert Stream to List
- Java 8 - BiFunction example
- Java 8 - Function example
- Java - Convert List to Map
- Exception testing in JUnit
- Hamcrest Collections Matcher
- Hamcrest equalTo () Matcher
- AAA pattern of unit test (Arrange/Act/Assert)
- Hamcrest Text Matcher
- Hamcrest Custom Matcher
- Why Junit uses Hamcrest
- Java - ForkJoinPool
- Java - How to use Futures
- Java - Simple HashTable implementation
- Java - Create a file in a specific path
- Java - Mockito의 @Mock, @Spy, @Captor, @InjectMocks
- Java - How to write test code using Mockito
- Java - Synchronized block
- Java - How to decompile a ".class" file into a Java file (jd-cli decompiler)
- Java - How to generate a random number
- Java - Calculate powers, Math.pow()
- Java - Calculate the square root, Math.sqrt()
- Java - How to compare String (==, equals, compare)
- Java - Calculate String Length
- Java - case conversion & comparison insensitive (toUpperCase, toLowerCase, equalsIgnoreCase)