Java 日历对象检索错误的日期?
/**
* Gets an instance of GMT Calendar.
*
* @return a new <code>Calendar</code> object
*/
public static Calendar getGMTCalendar()
{
return Calendar.getInstance(TimeZone.getTimeZone("GMT"), Locale.US);
}
/**
* @return The month (eg. Calenday.MAY) value for the date.
*/
public static int getMonth(Date date)
{
Calendar c = getGMTCalendar();
System.out.println("date ::"+date.toString());
c.setTime(date);
int month=c.get(Calendar.MONTH);
System.out.println("date after calendar is set ::"+c.getTime()+",Month="+month);
return c.get(Calendar.MONTH);
}
上面是我正在使用的代码片段。当我尝试检索传递给 getMonth() 的日期的月份时,如
int Month=DateUtil.getMonth(date); (日期是 10-01-2010)
我预计的月份是 8(一月 0 日,二月 1 日...九月 8 日,十月 9 日),但我在这里得到的是 月=8。 我尝试调试,发现在函数 getMonth() 中,当我使用设置时间时 c.setTime(date) 将日期设置为 09-31-2010,即过去日期的前一天。 我使用 INtellij Idea 作为 Ide 有人可以帮忙吗?
/**
* Gets an instance of GMT Calendar.
*
* @return a new <code>Calendar</code> object
*/
public static Calendar getGMTCalendar()
{
return Calendar.getInstance(TimeZone.getTimeZone("GMT"), Locale.US);
}
/**
* @return The month (eg. Calenday.MAY) value for the date.
*/
public static int getMonth(Date date)
{
Calendar c = getGMTCalendar();
System.out.println("date ::"+date.toString());
c.setTime(date);
int month=c.get(Calendar.MONTH);
System.out.println("date after calendar is set ::"+c.getTime()+",Month="+month);
return c.get(Calendar.MONTH);
}
Above is the code snipped I am using.When I try retrieving the month of the date I am passing to getMonth() like
int month=DateUtil.getMonth(date); (date is 10-01-2010)
The month I am expecting is 8(0-Jan,1-Feb...8-Sept,9-oct) but what I get here is
month=8.
I tried debugging and found that in the function getMonth() when i set the time using
c.setTime(date) the date is set to 09-31-2010 i.e a day before the date passed.
I am using INtellij Idea as the Ide
Can anyone help??
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
好的,现在我们已经得到了真实数据,问题出在您的初始日期上。
就我个人而言,我发现使用 Joda Time 诊断此类事情最简单:
这肯定会显示 UTC 中的所有内容......并且打印 2010-09-30T18:30:00.000Z。所以你进入日历的那一刻是九月,而不是十月。它为您提供的数据提供了正确的结果,因为您指定了 GMT 时区 - 您只是没有提供您认为的数据。
您需要了解,
Date
仅代表时间上的瞬间,与日历系统或时区无关。因此,虽然由于您的默认时区,该日期值为您打印为 10 月 1 日,但它实际上仍然是 ISO 日历中 UTC 9 月 30 日晚上的时刻。老实说,如果您可以使用 Joda Time 而不是内置日历类型,那么您应该这样做。 ..但您仍然需要小心当您不希望时应用系统默认时区。
Okay, now we've got the real data out, the problem is with your initial date.
Personally I find it easiest to diagnose this sort of thing using Joda Time:
That will definitely show everything in UTC... and it prints 2010-09-30T18:30:00.000Z. So the instant you're passing into the calendar is in September, not October. It's giving you the right result for the data you're giving it, as you've specified a GMT time zone - you're just not giving it the data you thought you were.
You need to understand that
Date
just represents an instant in time, with no reference to calendar system or time zone. So while that date value printed out October 1st for you because of your default time zone, it really is still the instant which is in the evening of September 30th in UTC in the ISO calendar.To be honest, if you can possibly use Joda Time instead of the built-in calendar types, you should... but you still need to be careful of the system default time zone being applied when you don't want it to be.
如果您位于具有负偏移量的时区(例如,美国)并将 GMT 日期设置为每月的第一天,时间为 00:00:00,则当地时间将是前一天(并且上个月)。检查并确保您没有从日历对象中检索当地时间。
If you're in a timezone with a negative offset (i.e. the US, for example) and set the GMT date to the first day of a month with time 00:00:00, the local time will be in the previous day (and previous month). Check to make sure you aren't retrieving the local time from the Calendar object.