‘壹’ java月份相减
/**
* @param args
*/
public static Integer getDiffNum(Date startMonth,Date endMonth){
Integer monthNum = 0;
Integer yearNumber = 0;
Calendar startCalendar = Calendar.getInstance();
Calendar endCalendar = Calendar.getInstance();
startCalendar.setTime(startMonth);
endCalendar.setTime(endMonth);
yearNumber = endCalendar.get(endCalendar.YEAR) - startCalendar.get(endCalendar.YEAR);
monthNum = endCalendar.get(Calendar.MONTH) - startCalendar.get(Calendar.MONTH);
return yearNumber*12 + monthNum;
}
已经测试
‘贰’ java链堜唤寰鐜锛屾疮链堥兘钖戝悗寰鐜6涓链堬纴姣斿傜幇鍦ㄦ槸5链堬纴钖戝悗灏辨槸铡诲勾镄12链堜唤锛岃繖镙风殑寰鐜镐庝箞鍐欙纻姹傛寚鏁
publicstaticvoidmain(String[]args){
SimpleDateFormatsdf=newSimpleDateFormat("yyyy/MM/ddHH:mm:ss");
Datedate=newDate();
System.out.println(sdf.format(date));
System.out.println(sdf.format(subtractMarch(date,6)));
}
//鍑忔湀浠
publicstaticDatesubtractMarch(Datedate,intnum){
Calendarcal=Calendar.getInstance();
cal.setTime(date);
cal.set(Calendar.MARCH,cal.get(Calendar.MARCH)-num);
returncal.getTime();
}