计算两个日期之间的差距_学习记录

计算两个日期之间的差距_学习记录

package p1;

import java.util.Scanner;

/**
 * 计算两个日期的差距实现
 * @author Guozhu zhu
 * @date 2018/7/21
 * @version 1.0
 *
 */
public class Test04 {
	
	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		System.out.println("================\n");
		int year01 = in.nextInt();
		int month01 = in.nextInt();
		int day01 = in.nextInt();
		System.out.println("=================\n");
		int year02 = in.nextInt();
		int month02 = in.nextInt();
		int day02 = in.nextInt();
		System.out.println("====input ok!====\n");
		int sum01 = 0;
		int sum02 = 0;
		for (int i = 0; i < year01; i++) {
			if (isLeafYear(i)) {
				sum01 += 366;
			} else {
				sum01 += 365;
			}
		}
		if (isLeafYear(year01)) {
			int[] arr = getMonthsArray(true);
			    for (int i = 0; i < month01; i++) {
				    sum01 += arr[i];
			    }
		}
		sum01 += day01;	
		for (int i = 0; i < year02; i++) {
			if (isLeafYear(i)) {
				sum02 += 366;
			} else {
				sum02 += 365;
			}
		}
		if (isLeafYear(year02)) {
			int[] arr = getMonthsArray(false);
				for (int i = 0; i < month02; i++) {
					sum02 += arr[i];
				}
	    }
	    sum02 += day02;
	    System.out.println("====output====\n");
	    System.out.println(sum02 - sum01);
	}
	
	public static boolean isLeafYear(long year) {
		if ((year%4 == 0 && year%100 != 0) || year%400 == 0) {
			return true;
		} else {
			return false;
		}
	}
	
	public static int[] getMonthsArray(boolean bool) {
		if (bool == true) {
			return new int[] {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
		} else {
			return new int[] {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
		}
	}

}

猜你喜欢

转载自blog.csdn.net/weixin_37770023/article/details/81154402