Java日历设置不正确
我在使用 Java 日历时遇到了一些问题。我正在解析 txt 文件中的一些数据,并且需要创建一个日期。完成以下代码后:
tmpYear = Double.parseDouble(row[yearIndex]);
tmpMonth = Double.parseDouble(row[monthIndex]);
tmpDay = Double.parseDouble(row[dayIndex]);
if(timeIndex != -1)
tmpTime = Double.parseDouble(row[timeIndex]);
if(secondsIndex != -1)
tmpSeconds = Double.parseDouble(row[secondsIndex]);
我可以调试,看到变量如下: tmpYear == 2010
tmp月份 == 12
tmpDay == 30
tmpTime == 15(这是一天中的时间)
tmpSeconds == 0
但是当运行以下代码时:
cal.set((int)tmpYear,(int)tmpMonth,(int)tmpDay,(int)tmpTime,
(int)((tmpTime - (int)tmpTime)*100),(int)tmpSeconds);
System.out.println(cal.getTime().toString());
我得到这个输出:
Sun Jan 30 15:00:00 CST 2011
有人可以解释一下可能的原因是什么吗?预先感谢大家的帮助!
I'm having some trouble with Java's Calendar. I'm parsing some data from a txt file, and need to create a date. After completion of the following code:
tmpYear = Double.parseDouble(row[yearIndex]);
tmpMonth = Double.parseDouble(row[monthIndex]);
tmpDay = Double.parseDouble(row[dayIndex]);
if(timeIndex != -1)
tmpTime = Double.parseDouble(row[timeIndex]);
if(secondsIndex != -1)
tmpSeconds = Double.parseDouble(row[secondsIndex]);
I can debug and see that the variables are as follows:
tmpYear == 2010
tmpMonth == 12
tmpDay == 30
tmpTime == 15 (This is the hour of the day)
tmpSeconds == 0
But when running the following code:
cal.set((int)tmpYear,(int)tmpMonth,(int)tmpDay,(int)tmpTime,
(int)((tmpTime - (int)tmpTime)*100),(int)tmpSeconds);
System.out.println(cal.getTime().toString());
I'm getting this for an output:
Sun Jan 30 15:00:00 CST 2011
Can someone explain what a possible reason for this would be? Thank you all in advance for the help!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
月份索引为 0-11,而不是 1-12。
0 = 一月
1 = 二月
...
11 = 十二月
请改用
tmpMonth = value -1
。months are indexed 0-11 instead of 1-12.
0 = January
1 = February
...
11 = December
Use
tmpMonth = value -1
instead.我相信月份的值从 0 而不是 1 开始,因此它将 0 解释为 Jan,1 解释为 Feb ...,然后将 Jan 解释为 12。
I believe the month's value starts at 0 rather than 1 so it interprets 0 as Jan, 1 as Feb ... and then Jan again as 12.
从API:
From the API:
当您设置 Calendar.MONTH 字段时,它是从零开始的。 {一月=0...十二月=11}
When you set the Calendar.MONTH field, it is zero-based. {January=0... December=11}
原因很简单:日历 API 中的设计错误。这就是 JSR 310 即将推出的原因,以改进 Java 对日期的支持。
从技术上讲,该类的作者认为仅使用静态字段是很好的。因此,您需要做的是使用以下内容:
他们认为人们可能不需要日历的动态设置,就像您需要的那样(对于我们大多数人来说,这很重要)。
The reason is quite simple: design fault in the Calendar API. That's why the JSR 310 is on its way in order to improve the java support for dates.
Technically, the authors of the class thought it was good to use only static fields. So what you need to do is to use the following:
They didn't think that people might need dynamic settings to a calendar, just like you need (and most of us, for that matters).
月份值从 0(一月)到 11(十二月)。设置月份以获取 12 月时,尝试使用 ((int) tmpMonth) - 1。
The month values go from 0 (January) to 11 (December). Try using ((int) tmpMonth) - 1 when setting the month to get December.