Date date1 = null,date2=null;
SimpleDateFormat format = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
try {
date1= format.parse(jLabel2.getText());
} catch (ParseException ex) {
Logger.getLogger(Settlements.class.getName()).log(Level.SEVERE, null, ex);
}
try {
date2= format.parse(jLabel3.getText());
} catch (ParseException ex) {
Logger.getLogger(Settlements.class.getName()).log(Level.SEVERE, null, ex);
}
long subDateValue = date2.getTime()-date1.getTime();
long subValueinDays = subDateValue/(24 * 60 * 60 * 1000);
System.out.println(subValueinDays + "Days");
//我有一个异常:java.text.ParseException:无法解析的日期:“10/10/2014” jLabel1的= 10 /二千零十四分之一十 jlabel2 = 11 /二千零十四分之十
答案 0 :(得分:1)
异常:java.text.ParseException:是因为您在SimpleDateFormat()中指定的格式与您要解析的Date的String格式不同。
当你指定这个SimpleDateFormat(“MM / dd / yyyy HH:mm:ss”)时;它期望您的日期的String格式将是这样的,例如01/01/2014 11:23:45
使用此
SimpleDateFormat format = new SimpleDateFormat("MM/dd/yyyy", Locale.ENGLISH);