简体   繁体   English

帮助Java日历 - 它提供了一些奇怪的输出

[英]Help with Java Calendar — it is giving some strange output

I'm newish to Java and am trying to do somethings with dates. 我是Java的新手,我正在尝试用日期来做事。 First I started using the Date class, which I found out was mostly deprecated so I switched over to Calendar. 首先我开始使用Date类,我发现它基本上已被弃用,所以我切换到了Calendar。

Now I am getting weird values. 现在我得到了奇怪的价值观。 for example the Month value for December is 0, not 12. And on those Calendars where it is giving me 0 for December it is also moving the year ahead one year. 例如,12月的月值是0,而不是12。而在那些给我0月12日的日历上,它也提前了一年。

It's weird! 有点奇怪!

What am I missing? 我错过了什么?

Thanks for your help. 谢谢你的帮助。

-GG -GG

EDIT FOR AN EXAMPLE: 编辑示例:

So I am reading some line sin from a file such as this: Johnny Graham HF 12-2-1973 Black 所以我正在从这样的文件中读出一些线条:Johnny Graham HF 12-2-1973 Black

I parse it, and then for the Calendar I set: 我解析它,然后为我设置的日历:

int year = Integer.parseInt(stringVersionOfYear); // this value is 1973

Then later when I go to get the year back with a line like this: 后来当我用这样一条线去回去年份时:

calendar.get(Calendar.YEAR)

the value is 1974... And the month is 0 for cal.get(Calendar.MONTH) 值为1974 ...而cal.get(Calendar.MONTH)的月份为0

EDIT 2: 编辑2:

I am creating the Calendar like this: 我正在创建这样的日历:

Calendar outputCalendar = Calendar.getInstance();
outputCalendar.set(year, month, day);

The java.util.Date and Calendar classes are poorly designed (eg, the first day in a month is day 1 but the first month in a year is month 0). java.util.Date和Calendar类的设计很差(例如,一个月的第一天是第1天,但是一年中的第一个月是第0个月)。 Many projects use the Joda Time package instead. 许多项目使用Joda Time包。

Months values are 0 thru 11; 月值为0到11; set a month to 12 and the date gets "normalized", incrementing the year by one and setting the month to 0. This makes it easy to "add a month" without having to worry about handling the overflow at year end. 将一个月设置为12并且日期变为“标准化”,将年份递增1并将月份设置为0.这使得“添加一个月”变得容易,而不必担心在年末处理溢出。

EDIT: January=0, February=1,... December=11. 编辑:1月= 0,2月= 1,... 12月= 11。 When you set the month value to 12 you were asking for the 13th month, which got normalized to the first month of the following year. 当您将月份值设置为12时,您要求的是第13个月,这已经标准化为次年的第一个月。

Note that this normalization process happens in general -- Try to set the date to December 32nd and you'll get back January 1 of the following year. 请注意,此规范化过程通常会发生 - 尝试将日期设置为12月32日,并且您将在下一年的1月1日返回。 This means it's important to be careful when modifying individual fields of a Calendar object. 这意味着在修改Calendar对象的各个字段时要小心。 If you create a default Calendar on, say, January 31st and then want to modify it to contain, say February 5th, the order in which you set the fields is important. 如果您在1月31日创建默认日历,然后想要将其修改为包含,例如2月5日,则设置字段的顺序非常重要。 If you change the month first you'll be creating February 31st, which will get normalized to March 2nd or 3rd (depending on the leap year) and then when you set the day to 5 the result is March 5th, not February 5th. 如果您首先更改月份,那么您将在2月31日创建,这将被标准化为3月2日或3日(取决于闰年),然后当您将日期设置为5时,结果是3月5日,而不是2月5日。 You have the opposite problem in other cases, such as starting from any date in February and modifying to the 30th or 31st of any other month. 在其他情况下,您会遇到相反的问题,例如从2月份的任何日期开始,并修改为任何其他月份的30日或31日。 In that case doing the month first results in the same type of problem. 在这种情况下,首先执行月份会导致相同类型的问题。

The only safe way to modify a date is to use a method that sets all three values simultaneously, such as the set(int,int,int) method. 修改日期的唯一安全方法是使用同时设置所有三个值的方法,例如set(int,int,int)方法。

See this line? 看到这一行?

outputCalendar.set(year, month, day);

Just change it like this: 只需改变它:

outputCalendar.set(year, month - 1, day);

and then when you want to get the month, don't use this: 然后当你想要获得月份时,不要使用这个:

cal.get(Calendar.MONTH)

instead, use this: 相反,使用这个:

(1+(cal.get(Calendar.MONTH)))

It's a pain, but it will fix the problem (I think). 这是一个痛苦,但它解决问题(我认为)。

The main piece of advice I have for you is: read the API! 我给你的主要建议是:阅读API! Calendar is indeed not the pinnacle of intuitive API design, but it is definitely usable if you take the time to read the javadoc. 日历确实不是直观API设计的巅峰之作,但如果您花时间阅读javadoc,它绝对可用。 Learn the difference between .add and .roll. 了解.add和.roll之间的区别。 Check out what happens when you set a year (1973) into a Calendar initialized with the current date (default Calendar.getInstance). 查看将年份(1973)设置为使用当前日期初始化的日历(默认为Calendar.getInstance)时会发生什么。

Whining about API is all fine (we all do it), but in the end to find solutions start off by reading what the authors provided to you before asking relatively obtuse questions on the Internet. 关于API的问题很好(我们都这么做),但最终通过阅读作者在互联网上提出相对迟钝的问题之前提供给你的内容来开始寻找解决方案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM