問題描述
我在使用 Java 的日歷時遇到了一些問題.我正在解析 txt 文件中的一些數(shù)據(jù),并且需要創(chuàng)建一個日期.完成以下代碼后:
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]);
我可以調(diào)試一下,看到變量如下:tmpYear == 2010
tmpMonth == 12
tmpDay == 30
tmpTime == 15(這是一天中的小時)
tmpSeconds == 0
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
但是在運(yùn)行以下代碼時:
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());
我得到這個是為了輸出:
2011 年 1 月 30 日星期日 15:00:00 CST
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!
推薦答案
月份索引為 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.
這篇關(guān)于Java 日歷設(shè)置不正確的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!