Calender设置固定时间遇到的问题
2016-12-30 17:18
459 查看
在使用Calender获取实例,设置成UTC时区时,发现比我们常规的月份多了一个月:
示例代码如下:
打印的结果为:
即2016-10-12 00:00:00,比我们设置的月份多了一个月。
我们来看下calender中set的源码:
看上面代码中飘红的部分,一月份是从0开始的~显而易见,需要将月份-1
示例代码如下:
Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone("UTC")); calendar.set(2016, 9, 11, 16, 0, 0); System.out.println(calendar.getTime());
打印的结果为:
Wed Oct 12 00:00:00 CST 2016
即2016-10-12 00:00:00,比我们设置的月份多了一个月。
我们来看下calender中set的源码:
/** * Sets the values for the fields <code>YEAR</code>, <code>MONTH</code>, * <code>DAY_OF_MONTH</code>, <code>HOUR_OF_DAY</code>, <code>MINUTE</code>, and * <code>SECOND</code>. * Previous values of other fields are retained. If this is not desired, * call {@link #clear()} first. * * @param year the value used to set the <code>YEAR</code> calendar field. * @param month the value used to set the <code>MONTH</code> calendar field. * Month value is 0-based. e.g., 0 for January. * @param date the value used to set the <code>DAY_OF_MONTH</code> calendar field. * @param hourOfDay the value used to set the <code>HOUR_OF_DAY</code> calendar field. * @param minute the value used to set the <code>MINUTE</code> calendar field. * @param second the value used to set the <code>SECOND</code> calendar field. * @see #set(int,int) * @see #set(int,int,int) * @see #set(int,int,int,int,int) */ public final void set(int year, int month, int date, int hourOfDay, int minute, int second) { set(YEAR, year); set(MONTH, month); set(DATE, date); set(HOUR_OF_DAY, hourOfDay); set(MINUTE, minute); set(SECOND, second); }
看上面代码中飘红的部分,一月份是从0开始的~显而易见,需要将月份-1
相关文章推荐
- 关于java时间设置时遇到的一个问题。
- 对近段时间遇到的c问题总结.
- 论坛升至discuz5.0时遇到了这个问题--交易手续费设置有误,请返回修改
- linux设置crontab时遇到的两个问题
- DNS 服务器遇到很多运行时间事件问题解决办法。
- 系统时间同步设置!解决Windows时间同步失败问题!
- 恢复phpAritcle2.0时遇到的Session设置的问题
- Mac 双系统的时间设置问题
- 时间控件Calender的设置
- C++调用matlab时可能遇到的问题和相关设置
- Asp.net开发环境的设置所遇到的问题
- 11.29 电驴设置对话框形式的实现中遇到的问题
- 在servlet设置javabean(最近学习j2ee遇到这个问题,在百度上找到的很好的回答)
- 求助 IIS 7 在设置脚本映射时遇到权限问题
- SQL SERVER2000 设置字段属性时遇到的问题
- 设置subversion遇到的一个意想不到的小问题
- DirectShow中用wm asf writer保存wmv文件遇到的媒体时间问题
- 终于搞定MSN不能登陆的问题了, 原来是我本机的时间设置不对,messenger登陆干嘛要验证客户端时间呢
- 设置邮件服务器遇到的问题
- s3c2410 MMU 启用后设置中断跳转指令遇到的问题