i am comparing two dates by both way(old and new)
public static void main(String[] args) throws ParseException {
// TODO Auto-generated method stub
long startTime1= Calendar.getInstance().getTimeInMillis();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date date1 = sdf.parse("2009-12-31");
Date date2 = sdf.parse("2010-01-31");
if(date1.before(date2)){
System.out.println("date1 is before d2");
}
long endTime1= Calendar.getInstance().getTimeInMillis();
long totalTime1 =endTime1-startTime1;
System.out.println(totalTime1);
long startTime2= Calendar.getInstance().getTimeInMillis();
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
LocalDate localDate1 = LocalDate.parse("2009-12-31",formatter);
LocalDate localDate2 = LocalDate.parse("2010-01-31",formatter);
if(localDate1.isBefore(localDate2)){
System.out.println("date1 is before d2");
}
long endTime2= Calendar.getInstance().getTimeInMillis();
long totalTime2 =endTime2-startTime2;
System.out.println(totalTime2);
}
output:
date1 is before d2
17//for using before
date1 is before d2
68//for using isBefore
so what is the best method for compairing because isBefore taking more time then before method but isBefore came in java 8 can anyone tell when we use isBefore?
I have also checked this but i want to know in case of performance
Should I use java.util.Date or switch to java.time.LocalDate