gpt4 book ai didi

java - 使用 Java GregorianCalendar 类打印正确的日期

转载 作者:搜寻专家 更新时间:2023-11-01 01:13:43 25 4
gpt4 key购买 nike

我正在尝试打印一些东西,例如今天的日期、星期几、从现在起 100 天后的日期、从现在起一百天后的星期几、我的生日和星期几一周的一周,以及我生日后的 10,000 天和一周的那一天。现在,我知道 GregorianCalendar 从一月份的 0 开始到十二月份的 11。我明白了,所以当我尝试打印日期时它说今天的日期是 2012 年 8 月 25 日而不是 2012 年 9 月 25 日是有道理的,但我不知道如何在不提前额外设置日期的情况下更正此问题月,然后实际上将月份放入十月而不是九月。

这是我目前正在处理的事情。

        GregorianCalendar cal = new GregorianCalendar();
int dayOfMonth = cal.get(Calendar.DAY_OF_MONTH);
int month = cal.get(Calendar.MONTH);
int year = cal.get(Calendar.YEAR);
int weekday = cal.get(Calendar.DAY_OF_WEEK);
cal.add(Calendar.DAY_OF_MONTH, 100);
int dayOfMonth2 = cal.get(Calendar.DAY_OF_MONTH);
int month2 = cal.get(Calendar.MONTH);
int year2 = cal.get(Calendar.YEAR);
int weekday2 = cal.get(Calendar.DAY_OF_WEEK);
GregorianCalendar birthday = new GregorianCalendar(1994, Calendar.JANUARY, 1);
int dayOfMonth3 = birthday.get(Calendar.DAY_OF_MONTH);
int month3 = birthday.get(Calendar.MONTH);
int year3 = birthday.get(Calendar.YEAR);
int weekday3 = birthday.get(Calendar.DAY_OF_WEEK);
birthday.add(Calendar.DAY_OF_MONTH, 10000);
int weekday4 = birthday.get(Calendar.DAY_OF_WEEK);
int dayOfMonth4 = birthday.get(Calendar.DAY_OF_MONTH);
int month4 = birthday.get(Calendar.MONTH);
int year4 = birthday.get(Calendar.YEAR);
System.out.printf("Todays date is " +month + "/" +dayOfMonth +"/" +year +".");
System.out.printf(" It is day " +weekday +" of the week");
System.out.println("");
System.out.printf("In 100 days it will be " +month2 + "/" +dayOfMonth2 +"/" +year2 +". ");
System.out.printf("Day " +weekday2 +" of the week");
System.out.println("");
System.out.printf("My Birthday is " +month3 + "/" +dayOfMonth3 +"/" +year3 +". "+"Day " +weekday3 +" of the week");
System.out.println("");
System.out.printf("10,000 days after my birthday is " +month4 + "/" +dayOfMonth4 +"/" +year4 +". " +"Day " +weekday4 +" of the week");

所以我需要帮助来更正今天日期、100 天后的日期、我的生日日期以及我生日后的 10,000 天的月份。非常感谢任何帮助或见解。

最佳答案

I get that, so it makes sense that when I attempt to print the date it says today's date is 8/25/12 rather than 9/25/12, but I have no idea how to correct this without setting the date ahead an extra month

如果你要通过做打印月份

int month = cal.get(Calendar.MONTH);
...
System.out.printf("Todays date is " + month + ...

然后你想打印 month + 1,而不仅仅是 month

尽管您最终会通过使用 SimpleDateFormat 来节省更多的时间和麻烦。用于将日期格式化为字符串。

关于java - 使用 Java GregorianCalendar 类打印正确的日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12592600/

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