简体   繁体   English

比较java中的日期会给出错误的结果

[英]Comparing dates in java is giving wrong results

I'm comparing dates using 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 printing false , but I'm expecting it to print true . 它打印false ,但我希望它打印true

can you please explain me what I'm doing wrong in this code? 你可以解释一下我在这段代码中做错了什么吗?

However the above code is working fine for below dates and printing true value: 但是,上面的代码适用于以下日期并打印真实值:

String sCreatedDate = "23.07.201507:25:35";
String sModifiedDate = "23.07.201507:26:07";

At the end of your format, you've used MM (months) instead of mm (minutes). 在格式结束时,您使用了MM (月)而不是mm (分钟)。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM