大概判断了一下,比较粗糙,仅供参考。
private static void function17() throws Exception {
Scanner sc=new Scanner(System.in);
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
System.out.println("请输入起始时间");
Date date1=sdf.parse(sc.next());
System.out.println("请输入截止时间");
Date date2=sdf.parse(sc.next());
long time1=date1.getTime();
long time2=date2.getTime();
long time=(time2-time1)/1000;
long year=time/(24*3600*365);
long month=time%(24*3600*365)/(24*3600*30);
if(year>0){
System.out.println(year+"年"+month+"月");
}else{
System.out.println(month+"月");
}
}
温馨提示:答案为网友推荐,仅供参考