How to compare two string dates in Java?

后端 未结 8 1349
刺人心
刺人心 2020-12-15 22:41

I have two dates in String format like below -

String startDate = \"2014/09/12 00:00\";

String endDate = \"2014/09/13 00:00\";

I want to

8条回答
  •  青春惊慌失措
    2020-12-15 23:36

    I think it could be done much simpler,

    Using Joda Time

    You can try parsing this dates simply:

    import org.joda.time.DateTime;
    import org.joda.time.format.DateTimeFormat;
    import org.joda.time.format.DateTimeFormatter;
    
    DateTimeFormatter formatter = DateTimeFormat.forPattern("yyyy/MM/dd HH:mm");
    DateTime d1 = formatter.parseDateTime(startDate);
    DateTime d2 = formatter.parseDateTime(endDate);
    
    Assert.assertTrue(d1.isBefore(d2));
    Assert.assertTrue(d2.isAfter(d1));
    

提交回复
热议问题