无法解析的日期。比较两个日期的解析异常
Unparseable date. Parse Exception comparing two dates
我正在尝试根据日期生成列表。我不断收到 Parse 异常。我以为我的日期格式完全相同。也许我错过了什么。我已经玩了几个小时但没有成功。任何帮助将不胜感激。
public static List<String> getMonthlyDates(Date startdate, Date enddate) {
List<String> dates = new ArrayList<String>();
Calendar calendar = new GregorianCalendar();
calendar.setTime(startdate);
while (calendar.getTime().before(enddate)) {
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
String result = df.format(calendar.getTime());
dates.add(result);
calendar.add(Calendar.MONTH, 1);
}
return dates;
}
Calendar Mcalendar = Calendar.getInstance();
String dd = editbillduedate.getText().toString();
//Which shows correctly in the format of 2015-Jun-15
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
//it makes no difference if format is yyyy-mm-dd
Date date;
try {
//THIS LINE BELOW IS WHERE I AM GETTING THE ERROR
date = format.parse(dd);
Mcalendar.add(Calendar.DAY_OF_YEAR, 365);
final Date newDate2 = Mcalendar.getTime();
List<String> dateList = getMonthlyDates(date, newDate2);
for (final String d1 : dateList) {
//Does something irrelevant
}
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
如果您想要解析与 2015-Jun-15
相同格式的日期,您应该将 SimpleDateFormat
更改为月份元素包含三个字符,如下所示:yyyy-MMM-dd
。所以改变这个:
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
至:
SimpleDateFormat format = new SimpleDateFormat("yyyy-MMM-dd");
还有这个:
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
至:
SimpleDateFormat df = new SimpleDateFormat("yyyy-MMM-dd");
我正在尝试根据日期生成列表。我不断收到 Parse 异常。我以为我的日期格式完全相同。也许我错过了什么。我已经玩了几个小时但没有成功。任何帮助将不胜感激。
public static List<String> getMonthlyDates(Date startdate, Date enddate) {
List<String> dates = new ArrayList<String>();
Calendar calendar = new GregorianCalendar();
calendar.setTime(startdate);
while (calendar.getTime().before(enddate)) {
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
String result = df.format(calendar.getTime());
dates.add(result);
calendar.add(Calendar.MONTH, 1);
}
return dates;
}
Calendar Mcalendar = Calendar.getInstance();
String dd = editbillduedate.getText().toString();
//Which shows correctly in the format of 2015-Jun-15
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
//it makes no difference if format is yyyy-mm-dd
Date date;
try {
//THIS LINE BELOW IS WHERE I AM GETTING THE ERROR
date = format.parse(dd);
Mcalendar.add(Calendar.DAY_OF_YEAR, 365);
final Date newDate2 = Mcalendar.getTime();
List<String> dateList = getMonthlyDates(date, newDate2);
for (final String d1 : dateList) {
//Does something irrelevant
}
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
如果您想要解析与 2015-Jun-15
相同格式的日期,您应该将 SimpleDateFormat
更改为月份元素包含三个字符,如下所示:yyyy-MMM-dd
。所以改变这个:
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
至:
SimpleDateFormat format = new SimpleDateFormat("yyyy-MMM-dd");
还有这个:
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
至:
SimpleDateFormat df = new SimpleDateFormat("yyyy-MMM-dd");