我想根據日期產生一個列表。我不斷收到一個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();
}
你成爲這一行什麼樣的字符串'字符串dd = editbillduedate.getText()。toString();'?你可以請調試並粘貼「dd」變量的值嗎? –
java.text.ParseException:Unparseable date:「2015-Jun-15」(at offset 5) – SmulianJulian
我想我明白了,請看看下面的答案:) –