Java日期方法运用小程序,输入开始日期和相加的日期,得到目标日期。
public static void main(String[] args) throws ParseException {
boolean flag = true;
while(flag){
DateFormat df = new SimpleDateFormat("yyyyMMdd");
System.out.println("输入开始时间,格式为:yyyyMMdd");
String bgTime = new Scanner(System.in).next();
Date date = df.parse(bgTime);
Calendar cal = Calendar.getInstance();
cal.setTime(date);
System.out.println("请输入相加天数");
int addDays = new Scanner(System.in).nextInt();
cal.add(Calendar.DAY_OF_MONTH, addDays);
Date time=cal.getTime();
System.out.println(bgTime+"加上" + addDays + "天后为:" + df.format(time));
System.out.println("需要继续计算吗? y-继续、n-退出");
String selectForYN = new Scanner(System.in).next();
if(selectForYN.equals("y")){
System.out.println("-----------------------");
flag = true;
}else if(selectForYN.equals("n")){
System.out.println("Thank You! bye bye!");
flag = false;
}else{
System.out.println("您的输入有误!自动退出");
flag = false;
}
}
}