2014-09-27 35 views
0
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:不可解析日期: “2014年10月10日” jLabel1的= 10 /二千零十四分之十 jlabel2 = 2014年11月10日如何。减去两个日期(在JLabel的集合)

+0

的可能重复【如何分析日期?] (http://stackoverflow.com/questions/999172/how-to-parse-a-date) – 2014-09-27 17:17:57

+0

可能重复此[上一个问题](http://stackoverflow.com/q/26064109/230513)。你尝试过'LocalDate'和'Period'吗? – trashgod 2014-09-27 17:52:41

回答

1

异常:java.text.ParseException:是因为您在SimpleDateFormat()中指定的格式与您正在解析的日期的字符串格式不同。

当您指定此SimpleDateFormat(“MM/dd/yyyy HH:mm:ss”);它预期的日期字符串格式将是这样的,例如2014年1月1日11时23分45秒

使用这一个

SimpleDateFormat format = new SimpleDateFormat("MM/dd/yyyy", Locale.ENGLISH); 
+0

是的,它的工作。谢谢。 – 2014-09-27 15:13:31

+0

如果它解决了您的问题,您必须接受该答案。这可能对别人有帮助。 – Naili 2014-09-27 15:16:56