Java 日历设置不正确

Java Calendar Setting Incorrectly(Java 日历设置不正确)
本文介绍了Java 日历设置不正确的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我在使用 Java 的日历时遇到了一些问题.我正在解析 txt 文件中的一些数据,并且需要创建一个日期.完成以下代码后:

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]);

我可以调试一下,看到变量如下: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

但是在运行以下代码时:

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.

这篇关于Java 日历设置不正确的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

How to implement RecyclerView with section header depending on category?(如何根据类别实现带有节标题的 RecyclerView?)
How to generate JNI header file in Eclipse(如何在 Eclipse 中生成 JNI 头文件)
Setting a custom HTTP header dynamically with Spring-WS client(使用 Spring-WS 客户端动态设置自定义 HTTP 标头)
Could you technically call the string[] anything in the main method?(从技术上讲,您可以在 main 方法中调用 string[] 吗?)
What is the proper way of setting headers in a URLConnection?(在 URLConnection 中设置标头的正确方法是什么?)
How to overwrite http-header quot;Hostquot; in a HttpURLConnection?(如何覆盖 http-header “主机在 HttpURLConnection 中?)