Java日历设置不正确

发布于 2024-10-10 11:50:42 字数 804 浏览 9 评论 0原文

我在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

做个ˇ局外人 2024-10-17 11:50:42

月份索引为 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.

转身泪倾城 2024-10-17 11:50:42

我相信月份的值从 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.

洒一地阳光 2024-10-17 11:50:42

从API:

月份 - 用于设置的值
月份时间字段。月值是
0 为基础。例如,0 代表一月。

From the API:

month - the value used to set the
MONTH time field. Month value is
0-based. e.g., 0 for January.

并安 2024-10-17 11:50:42

当您设置 Calendar.MONTH 字段时,它是从零开始的。 {一月=0...十二月=11}

When you set the Calendar.MONTH field, it is zero-based. {January=0... December=11}

浅笑轻吟梦一曲 2024-10-17 11:50:42

原因很简单:日历 API 中的设计错误。这就是 JSR 310 即将推出的原因,以改进 Java 对日期的支持。

从技术上讲,该类的作者认为仅使用静态字段是很好的。因此,您需要做的是使用以下内容:

calendar = ...
calendar.setMonth(Calendar.JANUARY);

他们认为人们可能不需要日历的动态设置,就像您需要的那样(对于我们大多数人来说,这很重要)。

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:

calendar = ...
calendar.setMonth(Calendar.JANUARY);

They didn't think that people might need dynamic settings to a calendar, just like you need (and most of us, for that matters).

把人绕傻吧 2024-10-17 11:50:42

月份值从 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文