0
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
try {
      Date startDate = format.parse(prodPeakChargeFormList.get(1).getStartDate());
      Date endDate = format.parse(prodPeakChargeFormList.get(1).getEndDate());
      if (format.parse(prod.getEndDate()) >= startDate
               && endDate >= format.parse(prod.getStartDate())) {
} catch(Exception e){
      e.printStackTrace();
}

>= cannot be applied to java.util.Date

how to compare date type in java?

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
k.sh
  • 5
  • 4
  • compareTo(), before() and after() methods are there. Try it. – Umesh Kumar Sharma Apr 24 '18 at 04:49
  • 1
    Make sure to read that second comment on the post that @AsthaSrivastava links to. [For why these classes were deprecated, see more here](https://stackoverflow.com/questions/2901262/why-were-most-java-util-date-methods-deprecated) – chb Apr 24 '18 at 04:57
  • You shouldn’t be using `SimpleDateFormat` and `Date`. Those classes are long outdated, and the former in particular also notoriously troublesome. Today we have so much better in [`java.time`, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/). Look into `LocalDateTime` and its `isBefore` or its `isAfter` method. – Ole V.V. Apr 24 '18 at 16:02

1 Answers1

0

You have compareTo method in Date class to perform that:

startDate.compareTo(endDate)

returns

1 which means startDate is greater than endDate 0 which means startDate is equal to endDate -1 which means startDate is less than endDate

piy26
  • 1,574
  • 11
  • 21
  • Even if you insist on using the outdated `Date` class this is the less readable way. That class has methods `before` and `after` that more clearly express the same (bus as I said in another comment, it’s better to use the modern `LocalDateTime` class). Also don’t count on those return values, they are not documented and may be different in another Java version. – Ole V.V. Apr 29 '18 at 04:51