- Java code
public void lentdvd (){ /////借出DVD的类 System.out.println("可以借出的DVD:"); for (int w=0;w<dvd.length;w++){ if (dvd[w]!=null){ System.out.println(dvd[w]); } } System.out.println("请输入需要借出的DVD:"); String lentname = sc.next(); System.out.println("请输入借出的日期(以-号分隔,参照:2012-06-01):"); lentri = sc.next(); //借出的日期 System.out.println("请输入归还的日期(以-号分隔,参照:2012-06-01):"); lentri1 = sc.next(); //归还的日期 }
用什么类来计算这两个日期的时间差比较好?请直接上代码,谢谢了。
------解决方案--------------------
- Java code
DateFormat df = new SimpleDateFormat("yyyy-MM-dd"); Date d1 = df.parse(lentri); Date d2 = df.parse(lentri1); long diff = d1.getTime() - d2.getTime(); long days = diff / (1000 * 60 * 60 * 24);
------解决方案--------------------
- Java code
final double MILLS_TO_DAY = 1000d * 60 * 60 * 24; String sBorrow = "2011-06-01"; String sReturn = "2012-06-01"; // 可以加一些格式验证 SimpleDateFormat formater = new SimpleDateFormat("yyyy-MM-dd"); Date borrowDate = null; Date returnDate = null; try { borrowDate = formater.parse(sBorrow); returnDate = formater.parse(sReturn); } catch (ParseException e) { e.printStackTrace(); } System.out.println((returnDate.getTime() - borrowDate.getTime()) / MILLS_TO_DAY);