2

This code should give false since 11:49 is before 12:07. But code is returning true.

If I change 12:07 to 13:00 it gives false which is correct. I have no idea what is wrong with 12:07. Am I missing something? I tried compareTo and giveTime methods as well with same results.

SimpleDateFormat format = new SimpleDateFormat("MM/dd/yyyy hh:mm");
System.out.println(format.parse("5/31/2018 11:49").after(format.parse("5/31/2018 12:07")));
xingbin
  • 27,410
  • 9
  • 53
  • 103
Bits Please
  • 318
  • 1
  • 11
  • 1
    I suspect 12:07 is 12:07 AM, just seven minutes after midnight. You'll have to specify AM or PM or find some other way to tell the system that some dates are to be assumed as PM. – markspace Jun 01 '18 at 16:25
  • I recommend you avoid the `SimpleDateFormat` class. It is not only long outdated, it is 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/). – Ole V.V. Jun 04 '18 at 07:18

4 Answers4

7

hh (ranges 1-12), 12:07 is parsed as 00:07:

SimpleDateFormat format = new SimpleDateFormat("MM/dd/yyyy hh:mm");
System.out.println(format.parse("5/31/2018 00:07").equals(format.parse("5/31/2018 12:07")));  // true

Use HH (ranges 0-23) instead, it will produce desired result:

SimpleDateFormat format = new SimpleDateFormat("MM/dd/yyyy HH:mm");
System.out.println(format.parse("5/31/2018 11:49").after(format.parse("5/31/2018 12:07"))); // false
achAmháin
  • 4,176
  • 4
  • 17
  • 40
xingbin
  • 27,410
  • 9
  • 53
  • 103
3

The "hh" is a 12 hour clock so "12:07" in interpreted as "12:07 AM". You probably want "HH". See https://docs.oracle.com/javase/8/docs/api/java/text/SimpleDateFormat.html

JustinKSU
  • 4,875
  • 2
  • 29
  • 51
3

You are missing something in the Format.

The hh format is for Hour in am/pm (1-12) as you could see in the documentation: https://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html

If you run the following:

System.out.println(format.parse("5/31/2018 12:07"));

You'll get:

Thu May 31 00:07:00 ART 2018

Which is why you're getting true.

You should change the time format to: HH:mm. That would be enough.

1

In addition to the other answers, you can make such hidden problems more easy to catch by calling setLenient(false) on your SimpleDateFormat object.

Per default the parsing process is lenient, i.e. parsing succeeds, even if a String doesn't fully match the pattern.

You wrote that writing "13" in the hour part worked fine, adding to your confusion. With lenient set to false, parse would throw a ParseException, because "13" doesn't match "hh", making it much more obvious that your String doesn't match the pattern.

SimpleDateFormat format = new SimpleDateFormat("MM/dd/yyyy hh:mm");
format.setLenient(false);
// Next line will throw a ParseException, as the second call to parse now fails
System.out.println(format.parse("5/31/2018 11:49").after(format.parse("5/31/2018 13:07")));
Max Vollmer
  • 8,412
  • 9
  • 28
  • 43
  • It throws a `ParseException` (not a `NullPointerException`) in your case of trying to parse `13:07`, but not in the case of `12:07` as in the question. So even though a slight improvement IMHO it’s not really to the point. For comparison the modern `DateTimeFormatter` from `java.time` will throw an exception always if you try to obtain date and time, not only for times after 13:00. I consider that much more helpful. – Ole V.V. Jun 04 '18 at 07:26
  • @OleV.V. You are right, I was thinking of `public Date parse(String text, ParsePosition pos)`, which returns `null` on error. – Max Vollmer Jun 04 '18 at 09:43