[Java] Java根据给定的日期计算其前一天和后一天的日期 →→→→→进入此内容的聊天室

来自 , 2020-11-10, 写在 Java, 查看 124 次.
URL http://www.code666.cn/view/2130eb64
  1. package com.xtgd.test;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.InputStreamReader;
  5.  
  6. public class Test {
  7.  
  8.     public static void main(String[] args) {
  9.         try {
  10.             BufferedReader br = new BufferedReader(new InputStreamReader(
  11.                     System.in));
  12.  
  13.             System.out.println("请输入年份");
  14.  
  15.             String s1 = br.readLine();
  16.  
  17.             System.out.println("请输入月份");
  18.  
  19.             String s2 = br.readLine();
  20.  
  21.             System.out.println("请输入日份");
  22.  
  23.             String s3 = br.readLine();
  24.  
  25.             int year = Integer.parseInt(s1);
  26.  
  27.             int month = Integer.parseInt(s2);
  28.  
  29.             int day = Integer.parseInt(s3);
  30.  
  31.             if (year >= 0 && month >= 1 && month <= 12) {
  32.  
  33.                 if (month == 1 || month == 3 || month == 5 || month == 7
  34.  
  35.                 || month == 8 || month == 10 || month == 12) {
  36.  
  37.                     if (day >= 1 && day <= 31) {
  38.  
  39.                         System.out.println("您输入的日期为:" + year + "年" + month
  40.                                 + "月"
  41.  
  42.                                 + day + "日");
  43.  
  44.                         if (day == 31) {
  45.  
  46.                             if (month == 12)
  47.  
  48.                                 System.out.println("且上一天为:" + year + "年12月"
  49.  
  50.                                 + (day - 1) + "日" + "," + "下一天为:"
  51.  
  52.                                 + (year + 1) + "年1月1日");
  53.  
  54.                             else
  55.  
  56.                                 System.out.println("且上一天为:" + year + "年"
  57.                                         + month
  58.  
  59.                                         + "月" + (day - 1) + "日" + "," + "下一天为:"
  60.  
  61.                                         + year + "年" + (month + 1) + "月1日");
  62.  
  63.                         } else if (day == 1) {
  64.  
  65.                             if (month == 1)
  66.  
  67.                                 System.out.println("且上一天为:" + (year - 1)
  68.  
  69.                                 + "年12月31日" + "," + "下一天为:" + year + "年"
  70.  
  71.                                 + month + "月" + (day + 1) + "日");
  72.  
  73.                             else if (month == 3) {
  74.  
  75.                                 if ((year % 4 == 0 && year % 100 != 0)
  76.  
  77.                                 || (year % 400 == 0))
  78.  
  79.                                     System.out.println("且上一天为:" + year
  80.                                             + "年2月29日"
  81.  
  82.                                             + "," + "下一天为:" + year + "年"
  83.                                             + month
  84.  
  85.                                             + "月" + (day + 1) + "日");
  86.  
  87.                                 else
  88.  
  89.                                     System.out.println("且上一天为:" + year
  90.                                             + "年2月28日"
  91.  
  92.                                             + "," + "下一天为:" + year + "年"
  93.                                             + month
  94.  
  95.                                             + "月" + (day + 1) + "日");
  96.  
  97.                             } else
  98.  
  99.                                 System.out.println("且上一天为:" + year + "年"
  100.  
  101.                                 + (month - 1) + "月30日" + "," + "下一天为:"
  102.  
  103.                                 + year + "年" + month + "月" + (day + 1)
  104.  
  105.                                 + "日");
  106.  
  107.                         } else
  108.  
  109.                             System.out.println("且上一天为:" + year + "年" + month
  110.                                     + "月"
  111.  
  112.                                     + (day - 1) + "日" + "," + "下一天为:" + year
  113.                                     + "年"
  114.  
  115.                                     + month + "月" + (day + 1) + "日");
  116.  
  117.                     } else
  118.  
  119.                         System.out.println("您输入的日期不合法");
  120.  
  121.                 } else if (month == 4 || month == 6 || month == 9
  122.                         || month == 11) {
  123.  
  124.                     if (day >= 1 && day <= 30) {
  125.  
  126.                         System.out.println("您输入的日期为:" + year + "年" + month
  127.                                 + "月"
  128.  
  129.                                 + day + "日");
  130.  
  131.                         if (day == 30)
  132.  
  133.                             System.out.println("且上一天为:" + year + "年" + month
  134.                                     + "月"
  135.  
  136.                                     + (day - 1) + "日" + "," + "下一天为:" + year
  137.                                     + "年"
  138.  
  139.                                     + (month + 1) + "月1日");
  140.  
  141.                         else if (day == 1)
  142.  
  143.                             System.out.println("且上一天为:" + year + "年"
  144.                                     + (month - 1)
  145.  
  146.                                     + "月31日" + "," + "下一天为:" + year + "年"
  147.                                     + month
  148.  
  149.                                     + (day + 1) + "日");
  150.  
  151.                         else
  152.  
  153.                             System.out.println("且上一天为:" + year + "年" + month
  154.                                     + "月"
  155.  
  156.                                     + (day - 1) + "日" + "," + "下一天为:" + year
  157.                                     + "年"
  158.  
  159.                                     + month + (day + 1) + "日");
  160.  
  161.                     } else
  162.  
  163.                         System.out.println("您输入的日期不合法");
  164.  
  165.                 } else if ((year % 4 == 0 && year % 100 != 0)
  166.                         || (year % 400 == 0)) {
  167.  
  168.                     if (day == 29)
  169.  
  170.                         System.out.println("您输入的日期为:" + year + "年" + month
  171.                                 + "月"
  172.  
  173.                                 + day + "日" + "n" + "且上一天为:" + year + "年2月28日"
  174.  
  175.                                 + "," + "下一天为:" + year + "年3月1日");
  176.  
  177.                     else {
  178.  
  179.                         if (day == 28)
  180.  
  181.                             System.out.println("您输入的日期为:" + year + "年" + month
  182.  
  183.                             + "月" + day + "日" + "n" + "且上一天为:" + year
  184.  
  185.                             + "年2月27日" + "," + "下一天为:" + year + "年2月29日");
  186.  
  187.                         else
  188.  
  189.                             System.out.println("您输入的日期不合法");
  190.  
  191.                     }
  192.  
  193.                 }
  194.  
  195.             } else {
  196.  
  197.                 System.out.println("您输入的日期不合法");
  198.             }
  199.         } catch (Exception e) {
  200.             e.printStackTrace();
  201.         }
  202.  
  203.     }
  204.  
  205. }
  206. //java/6805

回复 "Java根据给定的日期计算其前一天和后一天的日期"

这儿你可以回复上面这条便签

captcha