1 import java.text.SimpleDateFormat; 2 import java.util.Calendar; 3 import java.util.Scanner; 4 5 public class MyDate { 6 public static String nextdate(int month, int day, int year) { 7 if (1 <= month && month <= 12) 8 if (1 <= day && day <= 31) 9 if (1912 <= year && year <= 2050) { 10 SimpleDateFormat sFormat = new SimpleDateFormat("yyyyMMdd"); 11 sFormat.setLenient(false); 12 try { 13 Calendar c = Calendar.getInstance(); 14 c.setTime(sFormat 15 .parse("" + year + String.format("%02d", month) + String.format("%02d", day))); 16 c.add(Calendar.DATE, 1); 17 return c.get(Calendar.YEAR) + "年" + (c.get(Calendar.MONTH )+1) + "月" + c.get(Calendar.DATE) 18 + "日"; 19 } catch (Exception e) { 20 return "日期不存在"; 21 } 22 } else 23 return "年份超出范围"; 24 else 25 return "日期超出范围"; 26 else 27 return "月份超出范围"; 28 } 29 30 public static void main(String[] args) { 31 String input = ""; 32 Scanner scan = new Scanner(System.in); 33 while (true) { 34 System.out.print("请输入日期[日期格式yyyy MM dd]"); 35 input = scan.nextLine(); 36 String[] buf = input.split("\\s+"); 37 if (input.equals("")) 38 break; 39 else if (buf.length == 3) { 40 try { 41 int month = Integer.valueOf(buf[1]); 42 int date = Integer.valueOf(buf[2]); 43 int year = Integer.valueOf(buf[0]); 44 System.out.println(nextdate(month, date, year)); 45 } catch (Exception e) { 46 System.out.println("日期格式错误"); 47 continue; 48 } 49 } else { 50 System.out.println("日期格式错误"); 51 continue; 52 } 53 } 54 System.out.println("谢谢使用,再见~"); 55 scan.close(); 56 } 57 }
时间: 2024-10-15 14:21:26