java中的不可解析日期
我正试着和日期作比较 下面是我的java代码:java中的不可解析日期,java,android,Java,Android,我正试着和日期作比较 下面是我的java代码: SimpleDateFormat sdf = new SimpleDateFormat("EEEE MMMM dd, yyyy"); String currentDateString = sdf.format(new Date()); System.out.println(currentDateString); Date currentDate = sdf.parse(currentDateString);
SimpleDateFormat sdf = new SimpleDateFormat("EEEE MMMM dd, yyyy");
String currentDateString = sdf.format(new Date());
System.out.println(currentDateString);
Date currentDate = sdf.parse(currentDateString);
System.out.println("date:"+currentDate.toString());
sdf = new SimpleDateFormat("EEEE MMMM dd, yyyy");
Date weatherDate = sdf.parse("2014-04-04");
System.out.println("weatherDate:"+weatherDate);
if(currentDate.equals(weatherDate)){
System.out.println("true");
}
告诉我错误
Exception in thread "main" java.text.ParseException: Unparseable date: "2014-04-04"
at java.text.DateFormat.parse(Unknown Source)
at DateTime.main(DateTime.java:43)
请帮助我。您的
SimpleDateFormat
参数完全错误。更改它以匹配您提供的日期格式。2014-04-04
&EEEE MMMM dd,yyyy
彼此不匹配
2014-04-04
日期字符串与yyyy-MM-dd
日期模式完全匹配。日期“2014-04-04”
与您在SimpleDataFormat
中指定的格式不匹配。正确的格式应该是“yyyy-MM-dd”只需替换这一行即可
SimpleDateFormat sdf = new SimpleDateFormat("EEEE MMMM dd, yyyy");
与
还有这条线,
sdf = new SimpleDateFormat("EEEE MMMM dd, yyyy");
与
“2014-04-04”
不是“EEEE MMMM dd,yyyy”
您是否理解传递到SimpleDataFormat
构造函数中的字符串表示随后解析的数据格式?好的,那么如何将2014-04-04转换为“EEEE MMMM dd,yyyy”好的,然后如何将2014-04-04转换为“EEEE MMMM dd,yyyy”在这种情况下,首先需要使用yyyy-MM-dd
simple-date格式解析日期字符串,然后EEEE-MMMM-dd,yyyy
date模式将帮助您格式化日期。您需要两个格式化程序,一个用于解析,另一个用于再次格式化为字符串
sdf = new SimpleDateFormat("EEEE MMMM dd, yyyy");
sdf = new SimpleDateFormat("yyyy-MM-dd");