How to compare two dates along with time in java

后端 未结 6 1591
独厮守ぢ
独厮守ぢ 2020-11-29 07:30

I have two Date objects with the below format.

SimpleDateFormat sdf = new SimpleDateFormat(\"yyyy-MM-dd\'T\'HH:mm:ss\");
String         


        
6条回答
  •  清酒与你
    2020-11-29 07:59

    Since Date implements Comparable, it is as easy as:

    date1.compareTo(date2);
    

    As the Comparable contract stipulates, it will return a negative integer/zero/positive integer if date1 is considered less than/the same as/greater than date2 respectively (ie, before/same/after in this case).

    Note that Date has also .after() and .before() methods which will return booleans instead.

提交回复
热议问题