简体   繁体   English

无法比较两个Java日期,使用SimpleDateFormat“ dd / MM / YYYY hh:mm:ss a”将String转换为Date时转换无效

[英]Unable to compare two dates java,Invalid conversion while converting String to Date using SimpleDateFormat “dd/MM/YYYY hh:mm:ss a”

In my application I need to compare two different dates given in below formats. 在我的应用程序中,我需要比较以下格式的两个不同日期。

Inputs: there are 2 input dates in String format. 输入:有2个字符串格式的输入日期。

String actual="11/12/2012 11:26:04 AM";
String expected="21/12/2012 09:49:12 AM";

I am trying to use below java code for comparision. 我试图使用下面的Java代码进行比较。


SimpleDateFormat format= new SimpleDateFormat("dd/MM/YYYY hh:mm:ss a");
Date date1 = format.parse(actual);
System.out.println("Formated date1 is: "+format.format(date1).toString());       
// prints  :  01/01/2012 11:26:04 AM  Why????

Date date2= format.parse(expected);
System.out.println("Formated date2 is: "+format.format(date2).toString());        
// prints :  01/01/2012 09:49:12 AM  Why????

Calendar cal1 = Calendar.getInstance();
Calendar cal2 = Calendar.getInstance();
cal1.setTime(date1);
cal2.setTime(date2);
if( !(cal1.compareTo(cal2)<=0))
{
 result=false;
 String errMsg +="Actual:"+actual+" date is not before or equal to expected:"+expected+" date\n";
 System.out.println(errMsg);
}

But the above code is not working as expected. 但是上面的代码无法正常工作。 please check the wrong output mentioned in comments 请检查注释中提到的错误输出

I think there is something wrong with the formatting. 我认为格式有问题。 can anyone please help me. 谁能帮帮我吗。

your format should be : 您的格式应为:

SimpleDateFormat format= new SimpleDateFormat("dd/MM/yyyy hh:mm:ss a");

Notice year in lowercase y 通知year 小写y

Try: 尝试:

SimpleDateFormat format= new SimpleDateFormat("dd/MM/yyyy hh:mm:ss a");

The Ys should be lowercase. Y应该是小写的。

You can use Joda Time . 您可以使用Joda Time It has really nice methods, like isBefore() 它具有非常好的方法,例如isBefore()

在此处输入图片说明

String actual = "11/12/2012 11:26:04 AM";
String expected = "21/12/2012 09:49:12 AM";

DateTimeFormatter fmt = DateTimeFormat.forPattern("dd/MM/YYYY hh:mm:ss a");
DateTime dateTime1 = fmt.parseDateTime(actual);
DateTime dateTime2 = fmt.parseDateTime(expected);


if (dateTime1.isBefore(dateTime2)) {
    System.out.println("awesome");
}

暂无
暂无

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

相关问题 我如何才能比较具有以下规范“ java中的两个simpledateformat类型:dd / MM / yyyy hh:mm:ss a” - how can i compare two simpledateformat types in java with the following spec “dd/MM/yyyy hh:mm:ss a” 使用带有通配符支持的SimpleDateFormat解析日期字符串(例如* yyyy * MM * dd * hh * mm * ss) - Parsing date string using SimpleDateFormat with Wildcard support (e.g. *yyyy*MM*dd*hh*mm*ss) 在Java 7中将字符串日期转换为yyyy-MM-dd&#39;T&#39;HH:mm:ss.SSSSSS格式的字符串 - Converting string date to string in yyyy-MM-dd'T'HH:mm:ss.SSSSSS format in java 7 比较两个日期与时间,其中Formate是yyyy-MM-dd HH:mm:Android中的ss - Compare two dates with time whose Formate is yyyy-MM-dd HH:mm:ss in Android 如何比较日期-自定义格式为“ dd.MM.yyyy,HH:mm:ss”的字符串与另一个字符串? - How to compare a date - String in the custom format “dd.MM.yyyy, HH:mm:ss” with another one? 将字符串转换为“yyyy-MM-dd HH:mm:ss”时无法解析的日期异常 - Unparseable date exception when converting a string to “yyyy-MM-dd HH:mm:ss” 字符串(dd-MM-yyyy HH:mm)到日期(yyyy-MM-dd HH:mm)| Java的 - String (dd-MM-yyyy HH:mm) to Date (yyyy-MM-dd HH:mm) | Java 将日历对象转换为java格式的字符串,格式为“yyyy-mm-dd hh:mm:ss” - converting a calendar object into a string in java with format “yyyy-mm-dd hh:mm:ss” 在JAVA中将日期从字符串“ yyyyddmm hh:mm:ss”转换为字符串“ DD / MM / YYYY” - Convert date from string “yyyyddmm hh:mm:ss” to String “DD/MM/YYYY” in JAVA java.text.ParseException:无法解析的日期“ yyyy-MM-dd&#39;T&#39;HH:mm:ss.SSSXXX”-SimpleDateFormat - java.text.ParseException: Unparseable date “yyyy-MM-dd'T'HH:mm:ss.SSSXXX” - SimpleDateFormat
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM