完成以下要求 1:判断是否输入正确 2:判断输入的日期为星期几 3:判断输入的日期距离输入的第二个日期有多少天 4:判断输入日期的年龄
代码实现
package com.minrisoft;
import java.lang.String;
import java.util.Scanner;
public class Demo {
public static void calcWeek(
int a,
int b,
int c){
int x = a,y = b,z = c;
if(b ==
1){
b =
13;
a =a -
1;
}
if(b ==
12){
b =
14;
a = a -
1;
}
int h = (
int) ((c + (
26 * (b +
1) /
10) + (a %
100) + ((a %
100) /
4) + ((a /
100) /
4) +
5 * (a /
100)) %
7);
switch (h) {
case 0:
System.
out.println(x +
"年" + y +
"月" + z +
"日:星期六");
break;
case 1:
System.
out.println(x +
"年" + y +
"月" + z +
"日:星期天");
break;
case 2:
System.
out.println(x +
"年" + y +
"月" + z +
"日:星期一");
break;
case 3:
System.
out.println(x +
"年" + y +
"月" + z +
"日:星期二");
break;
case 4:
System.
out.println(x +
"年" + y +
"月" + z +
"日:星期三");
break;
case 5:
System.
out.println(x +
"年" + y +
"月" + z +
"日:星期四");
break;
case 6:
System.
out.println(x +
"年" + y +
"月" + z +
"日:星期五");
break;
}
}
public static boolean
leapYear(
int y){
if (y %
400 ==
0 || (y %
4 ==
0 && y %
100 !=
0))
return true;
else
return false;
}
public static int countDay(
int y,
int m,
int d){
int month_1[] ={
31,
29,
31,
30,
31,
30,
31,
31,
30,
31,
30,
31 };
int month_2[] ={
31,
28,
31,
30,
31,
30,
31,
31,
30,
31,
30,
31 };
int count =
0;
do {
count +=
365;
if (leapYear(y) ==
true)
count +=
1;
}
while((--y)!=
0);
do{
if (leapYear(y) ==
true)
count += month_1[m-
1];
else
count += month_2[m-
1];
}
while ((--m)!=
0);
count += d;
return count;
}
public static void Ture_or_False(
int x,
int y,
int z){
if (y >=
1 && y <=
12 && z >=
1 && z <=
31){
if (y ==
4 || y ==
6 || y ==
9 || y ==
11){
if (z >
30){
System.
out.println(
"输入错误");
System.
out.println(
">-----------------------------<");
System.exit(
0);
}
}
if (y ==
2){
if (leapYear(y) ==
true){
if (z >
29){
System.
out.println(
"输入错误");
System.
out.println(
">-----------------------------<");
System.exit(
0);
}
}
else{
if (z >
28){
System.
out.println(
"输入错误");
System.
out.println(
">-----------------------------<");
System.exit(
0);
}
}
}
}
else{
System.
out.println(
"输入错误");
System.
out.println(
">-----------------------------<");
System.exit(
0);
}
System.
out.println(x +
"年" + y +
"月" + z +
"日:输入正确");
}
public static void sumday(
int y1,
int y2,
int x,
int y,
int z,
int i,
int j,
int k){
if (y1 == y2)
System.
out.println(x +
"年" + y +
"月" + z +
"日:处于今天");
if (y1 < y2)
System.
out.println(x +
"年" + y +
"月" + z +
"日距离"+i +
"年" + j +
"月" + k +
"有" + (y2 - y1) +
"天");
if (y1 > y2){
System.
out.println(x +
"年" + y +
"月" + z +
"日距离"+i +
"年" + j +
"月" + k +
"有" + (y1 - y2) +
"天");
}
}
public static void ages(
int y1,
int y2,
int x,
int y,
int z){
if (y1 > y2){
System.
out.println(x +
"年" + y +
"月" + z +
"日:没有出生");
}
else{
int a =
2017 - x;
if (y >
4 || (x !=
2017 && y ==
4 && z >
10))
a -=
1;
System.
out.println(x +
"年" + y +
"月" +z +
"日:今年"+ a +
"岁");
}
}
public static void main(String [] args){
Scanner sc =
new Scanner(System.
in);
System.
out.print(
"请输入第一个日期(格式:yymmdd):");
String y_m_d_1 = sc.nextLine();
System.
out.println(
"请输入第二个日期(格式:yymmdd)");
System.
out.print(
"注意:保证在第一次输入的日期之后:");
String y_m_d_2 = sc.nextLine();
sc.close();
int x =Integer.parseInt(y_m_d_1.substring(
0,
4));
int y =Integer.parseInt(y_m_d_1.substring(
4,
6));
int z =Integer.parseInt(y_m_d_1.substring(
6,
8));
int a =Integer.parseInt(y_m_d_2.substring(
0,
4));
int b =Integer.parseInt(y_m_d_2.substring(
4,
6));
int c =Integer.parseInt(y_m_d_2.substring(
6,
8));
int year_1 = countDay(x,y,z);
int year_2 = countDay(a,b,c);
System.
out.println(
">------------查询结果----------<");
Ture_or_False(x,y,z);
calcWeek(x,y,z);
sumday(year_1,year_2,x,y,z,a,b,c);
ages(year_1,year_2,x,y,z);
System.
out.println(
"|------------------------------|");
}
}
转载请注明原文地址: https://ju.6miu.com/read-673016.html