What are various ways to compare dates in Java?

JavaServer Side ProgrammingProgramming

Using the LocalDate class

The java.time.LocalDate class represents the local date i.e. the date without time zone, you can use this object instead of Date. This class provides various methods such as isBefore(), isAfter() and, isEqual() to compare two dates −

Example

Live Demo

import java.time.LocalDate;
public class Sample {
   public static void main(String args[]) {  
      LocalDate date1 = LocalDate.of(2007, 11, 25);
      LocalDate date2 = LocalDate.of(1999, 9, 12);      
      Boolean bool1 = date1.isAfter(date2);  
      Boolean bool2 = date1.isBefore(date2);
      Boolean bool3 = date1.isEqual(date2);
      if(bool1){
         System.out.println(date1+" is after "+date2);
      }else if(bool2){
         System.out.println(date1+" is before "+date2);
      }else if(bool3){
          System.out.println(date1+" is equla to "+date2);
      }
   }
}

Output

2007-11-25 is after 1999-09-12

Using the Date class

The java.util.Date class represents a specific instant time This class provides various methods such as before(), after() and, equals() to compare two dates −

Example

Live Demo

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.util.Date;
public class Sample {
   public static void main(String args[]) throws ParseException {  
      SimpleDateFormat formatter = new SimpleDateFormat("yyyy-dd-MM");      
      //Parsing the given String to Date object
      Date date1 = formatter.parse("2007-11-25");  
      Date date2 = formatter.parse("1999-9-12");      
      Boolean bool1 = date1.after(date2);  
      Boolean bool2 = date1.before(date2);
      Boolean bool3 = date1.equals(date2);
      if(bool1){
         System.out.println(date1+" is after "+date2);
      }else if(bool2){
         System.out.println(date1+" is before "+date2);
      }else if(bool3){
          System.out.println(date1+" is equals to "+date2);
      }
   }
}

Output

Sun Jan 11 00:00:00 IST 2009 is after Thu Dec 09 00:00:00 IST 1999

Using the Calendar class

Similar to the Date class, the Calendar class provides various methods such as before(), after() and, equals() to compare two dates −

Example

Live Demo

import java.util.Calendar;
public class Sample {
   public static void main(String args[]) {  
      Calendar obj1 = Calendar.getInstance();
      obj1.set(2007, 11, 25);      
      Calendar obj2 = Calendar.getInstance();
      obj2.set(1999, 9, 15);
      Boolean bool1 = obj1.after(obj2);  
      Boolean bool2 = obj1.before(obj2);
      Boolean bool3 = obj1.equals(obj2);
      if(bool1){
         System.out.println(obj1+" is after "+obj2);
      }else if(bool2){
         System.out.println(obj1+" is before "+obj2);
      }else if(bool3){
          System.out.println(obj1+" is equla to "+obj2);
      }
   }
}

Output

java.util.GregorianCalendar[time=?,areFieldsSet=false,areAllFieldsSet=true,lenient=true,zone=sun.util.calendar.ZoneInfo[id="Asia/Calcutta",offset=19800000,dstSavings=0,useDaylight=false,transitions=7,lastRule=null],firstDayOfWeek=1,minimalDaysInFirstWeek=1,ERA=1,YEAR=2007,MONTH=11,WEEK_OF_YEAR=45,WEEK_OF_MONTH=1,DAY_OF_MONTH=25,DAY_OF_YEAR=312,DAY_OF_WEEK=7,DAY_OF_WEEK_IN_MONTH=1,AM_PM=1,HOUR=9,HOUR_OF_DAY=21,MINUTE=33,SECOND=47,MILLISECOND=366,ZONE_OFFSET=19800000,DST_OFFSET=0] is after java.util.GregorianCalendar[time=?,areFieldsSet=false,areAllFieldsSet=true,lenient=true,zone=sun.util.calendar.ZoneInfo[id="Asia/Calcutta",offset=19800000,dstSavings=0,useDaylight=false,transitions=7,lastRule=null],firstDayOfWeek=1,minimalDaysInFirstWeek=1,ERA=1,YEAR=1999,MONTH=9,WEEK_OF_YEAR=45,WEEK_OF_MONTH=1,DAY_OF_MONTH=15,DAY_OF_YEAR=312,DAY_OF_WEEK=7,DAY_OF_WEEK_IN_MONTH=1,AM_PM=1,HOUR=9,HOUR_OF_DAY=21,MINUTE=33,SECOND=47,MILLISECOND=392,ZONE_OFFSET=19800000,DST_OFFSET=0]

raja
Published on 06-Feb-2021 03:49:30
Advertisements