DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
try {
Date d1 = new Date();
Date d2 = df.parse("2018-03-02 11:30:24");
Calendar cal = Calendar.getInstance();
cal.setTime(d2);
int day = cal.get(Calendar.DAY_OF_MONTH);
int month = cal.get(Calendar.MONTH) ;
int year = cal.get(Calendar.YEAR);
if(month==11) {
month = 0;
year +=1;
}else {
month += 1;
}
day = 15;
cal.set(year, month, day);
//d2定位下个月的十五号
System.out.println(d1);
System.out.println(cal.getTime());//设置日历时间
long diff = cal.getTime().getTime() - d1.getTime(); //时间差
long days = diff / (1000 * 60 * 60 * 24);
System.out.println(days);
} catch (Exception e) {
e.printStackTrace();
}
try {
Date d1 = new Date();
Date d2 = df.parse("2018-03-02 11:30:24");
Calendar cal = Calendar.getInstance();
cal.setTime(d2);
int day = cal.get(Calendar.DAY_OF_MONTH);
int month = cal.get(Calendar.MONTH) ;
int year = cal.get(Calendar.YEAR);
if(month==11) {
month = 0;
year +=1;
}else {
month += 1;
}
day = 15;
cal.set(year, month, day);
//d2定位下个月的十五号
System.out.println(d1);
System.out.println(cal.getTime());//设置日历时间
long diff = cal.getTime().getTime() - d1.getTime(); //时间差
long days = diff / (1000 * 60 * 60 * 24);
System.out.println(days);
} catch (Exception e) {
e.printStackTrace();
}