java计算30天后日期
import java.util.Scanner; public class Work2 { public static void main(String []args){ Scanner in = new Scanner(System.in); int year = in.nextInt(); int month = in.nextInt(); int day = in.nextInt(); int left = 0; boolean leap; if( (year%4==0 && year%100!=0) || year%400==0){ leap = true; } switch(month){ case 1: case 3: case 5: case 7: case 8: case 10: left = 31 - day; if (left<30) { month++; day = left; } else day = 31;//输入为1号的特殊情况 break; case 4: case 6: case 9: case 11: left = 30 - day; month ++; day = left; break; case 12: left = 31 - day; if(left<30){ month++; year++; day = left; } else day = 31; break; case 2: if(leap = true){ left = 29- day; month = 3; day = left; } else{ left = 28 - day; month ++; day = left; } break; } System.out.println("该日期30天后的日期是:"+year+" "+month+" "+day); }
}
/*
先给定一个时间,例如2017年10月1日
*/
Calendar cld = Calendar.getInstance();
cld.set(Calendar.YEAR, 2017);
cld.set(Calendar.MONDAY,10);
cld.set(Calendar.DATE,1);
//调用Calendar类中的add(),增加时间量
cld.add(Calendar.DATE, 30);
System.out.println(year+"年"+month+"月"+date+"日");