输入某年某月某日,判断这一天是这一年的第几天(java代码)

public class Test {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入一个日期  ");
int y = sc.nextInt();
int m = sc.nextInt();
int t = sc.nextInt();
System.out.println(Tian(y, m, t));
}

public static String Tian(int y, int m, int t) {
String str = y + "年" + m + "月" + t + "日 ";
int sumt = 0;
if ((y % 4 == 0 && y % 100 != 0) || (y % 400 == 0)) {
int a[] = { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
if (m > 12 || m < 1)
return "输入的月份不正确";
else if (t > a[m - 1] || t < 1)
return "输入的日期不正确";
else {
sumt = t;
for (int i = 0; i < m - 1; i++) {
sumt = sumt + a[i];
}
}
str = str + "是: " + y + "年第" + sumt + "天";
return str;
} else {
int a[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
 
if (m > 12 || m < 1)
return "输入的月份不正确";
else if (t > a[m - 1] || t < 1)
return "输入的日期不正确";
else {
sumt = t;
for (int i = 0; i < m - 1; i++) {
sumt = sumt + a[i];
}
str = str + "是" + y + "年第" + sumt + "天";
return str;
}
}
}
}

猜你喜欢

转载自blog.csdn.net/IT_LuoSha/article/details/79773751