Comparing dates in java gives incorrect results
I am comparing dates using the following code
String sCreatedDate = "30.07.201514:57:03";
String sModifiedDate = "30.07.201515:40:34";
SimpleDateFormat parser = new SimpleDateFormat("dd.MM.yyyyHH:MM:SS");
Date d1 = parser.parse(sCreatedDate);
Date d2 = parser.parse(sModifiedDate);
System.out.println(d1.before(d2));
It prints false
, but I expect it to print true
.
Could you please explain to me what I am doing wrong in this code?
However, the above code works fine for below dates and prints the true value:
String sCreatedDate = "23.07.201507:25:35";
String sModifiedDate = "23.07.201507:26:07";
+3
source to share
1 answer
At the end of your format, you used MM
(months) instead of MM
(minutes).
String sCreatedDate = "30.07.201514:57:03";
String sModifiedDate = "30.07.201515:40:34";
SimpleDateFormat parser = new SimpleDateFormat("dd.MM.yyyyHH:mm:ss");//mm small
Date d1 = parser.parse(sCreatedDate);
Date d2 = parser.parse(sModifiedDate);
System.out.println(d1.before(d2));//true
+9
source to share