Package testService

Source Code of testService.GoodCode2

package testService;

import java.util.Scanner;

import model.MyDate;

public class GoodCode2 {
  private static int[] days29 = { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30,
      31 };
  private static int[] days28 = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30,
      31 };

  /**
   * @param args
   */
  @SuppressWarnings("unused")
  public static void main(String[] args) {
    Scanner in = new Scanner(System.in);
    int year, month, day;
    MyDate next = new MyDate();
    MyDate previous = new MyDate();
    try {
      year = in.nextInt();
      month = in.nextInt();
      day = in.nextInt();
    } catch (Exception e) {
      System.out.println("请输入正确的数字");
      return;
    }
    if (!isCorrect(year, month, day)) {
      System.out.println("请输入正确的日期");
      return;
    }

    if (!isFirst(year, month, day) && !isLast(year, month, day)) {
      next.setMonth(month);
      next.setYear(year);
      next.setDay(day + 1);
      previous.setYear(year);
      previous.setMonth(month);
      previous.setDay(day - 1);

    } else if (isFirst(year, month, day)) {
      if (!isFirstYear(year, month, day)) {
        previous.setYear(year);
        previous.setMonth(month - 1);
        if (isLeapYear(year)) {
          previous.setDay(days29[month - 2]);
        } else {
          previous.setDay(days28[month - 2]);
        }
      } else {
        previous.setYear(year - 1);
        previous.setMonth(12);
        previous.setDay(31);
      }
      next.setMonth(month);
      next.setYear(year);
      next.setDay(day + 1);
    } else if (isLast(year, month, day)) {
      if (!isLastYear(year, month, day)) {
        next.setMonth(month + 1);
        next.setYear(year);
        next.setDay(1);

      } else {
        next.setMonth(1);
        next.setYear(year + 1);
        next.setDay(1);
      }
      previous.setYear(year - 1);
      previous.setMonth(month);
      previous.setDay(day - 1);
    }
    System.out.println("下一天是" + next);
    System.out.println("前一天是" + previous);
    return;
  }

  public static boolean isCorrect(int year, int month, int day) {
    if (year < 1 || month < 1 || day < 1 || month > 12) {
      return false;
    }
    if (isLeapYear(year)) {
      if (day > days29[month - 1]) {
        return false;
      }
    } else {
      if (day > days28[month - 1]) {
        return false;
      }
    }
    return true;
  }

  public static boolean isFirst(int year, int month, int day) {
    return day == 1 ? true : false;
  }

  public static boolean isLast(int year, int month, int day) {
    if (isLeapYear(year)) {
      return day == days29[month - 1];
    } else {
      return day == days28[month - 1];
    }
  }

  public static boolean isFirstYear(int year, int month, int day) {
    return (day == 1 && month == 1) ? true : false;
  }

  public static boolean isLastYear(int year, int month, int day) {
    return (day == 31 && month == 12) ? true : false;
  }

  public static boolean isLeapYear(int year) {
    boolean result = false;
    if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) {
      result = true;
    }
    return result;
  }
}
TOP

Related Classes of testService.GoodCode2

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.