-2

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

Community
  • 1
  • 1
jitendra varshney
  • 3,484
  • 1
  • 21
  • 31
  • 5
    This is definitely not best way of doing performance measuring... – Andremoniy Feb 13 '17 at 10:30
  • so when we use isBefore() ? @Andremoniy – jitendra varshney Feb 13 '17 at 10:31
  • 1
    you use `isBefore()` when you use `java.time.LocalDate` and when you use `java.util.Date` you use `before()` – Selim Feb 13 '17 at 10:33
  • 1
    If your concern is performance, and you want to measure performance for micro calls, see this : http://stackoverflow.com/questions/504103/how-do-i-write-a-correct-micro-benchmark-in-java . But I doubt date comparison performance would matter seriously in any real-life application. Date parsing is seriously fast too. – GPI Feb 13 '17 at 10:34

2 Answers2

4

Use the new one. LocalDate, LocalTime and LocalDateTime all are immutable and thread-safe. They're safer to use.

Olivier Grégoire
  • 33,839
  • 23
  • 96
  • 137
Eric Chao
  • 41
  • 3
  • thanks @Eric for suggestion means you are saying before LocalDate date comparision is not safe – jitendra varshney Feb 13 '17 at 10:45
  • 2
    @jitendravarshney what is meant here is that the new API (`java.time`) is much, much better than what existed before (`java.util.Date`). If you use Java 8, use `java.time` and not `java.util.Date` or `java.util.Calendar` anymore. – Olivier Grégoire Feb 13 '17 at 10:47
  • ok @OlivierGrégoire Java8 DateTimeFormatter has great features as comparision to old Date – jitendra varshney Feb 13 '17 at 10:55
0

ChronoUnit is far more efficient when comparing dates. Here is the example which you can use:

    LocalDateTime timeNow = LocalDateTime.now();
    LocalDateTime timeAfterSometime = timeNow.plusHours(4).plusMinutes(11);

    System.out.println("timeNow = "+timeNow);
    System.out.println("timeAfterSometime = "+timeAfterSometime);

    long minutesDiff = ChronoUnit.MINUTES.between(timeNow, timeAfterSometime); // 251
    long hoursDiff = ChronoUnit.HOURS.between(timeNow, timeAfterSometime); // 4

    System.out.println("minutesDiff = "+minutesDiff);
    System.out.println("hoursDiff = "+hoursDiff);

And the output will be as follows:

timeNow = 2017-02-13T16:05:00.617 timeAfterSometime = 2017-02-13T20:16:00.617 minutesDiff = 251 hoursDiff = 4

KayV
  • 12,987
  • 11
  • 98
  • 148