gpt4 book ai didi

java - 如何打印给定日期年月日?

转载 作者:行者123 更新时间:2023-12-02 06:21:50 27 4
gpt4 key购买 nike

在此我得到闰年和普通年份和月份,但我不知道如何获取给定日期月份年份的日期。还有一个问题:1800年和1900年是平年,但我知道这些年是闰年。你能解决这个问题吗?

import java.util.Scanner;

class day {

public static void main(String arg[]) {
int tm, sm, w;
int y;
int[] m = new int[12];
m[0] = 31;
m[1] = 28;
m[2] = 31;
m[3] = 30;
m[4] = 31;
m[5] = 30;
m[6] = 31;
m[7] = 31;
m[8] = 30;
m[9] = 31;
m[10] = 30;
m[11] = 31;
//{31,28,31,30,31,30,31,31,30,31,30,31};
String[] mo = new String[12];
mo[0] = "January";
mo[1] = "February";
mo[2] = "March";
mo[3] = "April";
mo[4] = "May";
mo[5] = "June";
mo[6] = "July";
mo[7] = "August";
mo[8] = "September";
mo[9] = "October";
mo[10] = "November";
mo[11] = "December";
String[] we = new String[w];
we[0] = "Sunday";
we[1] = "Monday";
we[2] = "Tuesday";
we[3] = "Wednesday";
we[4] = "Thursday";
we[5] = "Friday";
we[6] = "Saturday";
Scanner ip = new Scanner(System.in);
System.out.print("\nEnter year ");
y = ip.nextInt();
System.out.print("\nEnter month ");
sm = ip.nextInt();
if (y % 4 == 0) {
m[1] = m[1] + 1;
for (tm = 1; tm <= m.length; tm++) {
if (tm == sm) {
System.out.print("\n" + y + " is a Leap Year\n"
+ mo[sm - 1] + " month " + "has " + m[sm - 1] + " days\n");
}
}
} else {
for (tm = 1; tm <= 12; tm++) {
if (tm == sm) {
System.out.print("\n" + y + " is an Ordinary year\n"
+ mo[sm - 1] + " month " + "has " + m[sm - 1] + " days\n");
}
}
}
}
}

最佳答案

这是如何识别公历的闰年

if ((y % 4 == 0  && y % 100 !=0) || y % 400 == 0) {
...

参见http://en.wikipedia.org/wiki/Leap_year

关于java - 如何打印给定日期年月日?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20943153/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com