我有两个Date
s。如何区分这两个日期的天数?
听说过SimpleDateFormat
,但是不知道怎么用。
我试过这个:
String fromdate = "Apr 10 2011";
SimpleDateFormat sdf;
sdf = new SimpleDateFormat("MMM DD YYYY");
sdf.parse(fromdate);
Calendar cal = Calendar.getInstance();
cal.setTime(sdf);
我也试过这个:
String date1 = "APR 11 2011";
String date2 = "JUN 02 2011";
String format = "MMM DD YYYY";
SimpleDateFormat sdf = new SimpleDateFormat(format);
Date dateObj1 = sdf.parse(date1);
Date dateObj2 = sdf.parse(date2);
long diff = dateObj2.getTime() - dateObj1.getTime();
int diffDays = (int) (diff / (24* 1000 * 60 * 60));
System.out.println(diffDays);
但我得到了例外“非法模式字符'Y'”。