Calender设置固定时间遇到的问题

摘要:
Calendarcalendar=Calendar.getInstance(TimeZone.getTimeZone(“UTC”));&lt,*首先调用{@link#clear()}。**@paramyear用于设置<年份<日历字段。*@parammonth用于设置&lt,

在使用Calender获取实例,设置成UTC时区时,发现比我们常规的月份多了一个月:

示例代码如下:

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

免责声明:文章转载自《Calender设置固定时间遇到的问题》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇二分法:CF371C-Hamburgers(二分法+字符串的处理)关于sql server 2008 R2中遇到的sql server 代理启动不了的解决过程下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

HTML中的表格标签

  表格是网页制作中使用最多的工具之一,在制作网页时,使用表格可以更清晰地排列数据。但是在实际制作过程中,表格更多用在网页布局的定位上。很多网页都是以表格布局的。这是因为表格在文本和图像的位置控制方面都有很强的功能。 表格的基本构成表格由行、列、和单元格3部分组成,一般通过3个标记来创建,分别是表格标记<table>、行标记<tr>...

Linux bash运维操作日志审计(单服务器)

目前公司有几台机器比较重要,需要把所有用户的操作记录下来,于是就是参照资料来完成 1. vim /etc/profile.d/oplogrc.sh logdir=/opt/oplog userdir=$logdir/${LOGNAME} DT=`date +"%Y%m%d"` export HISTFILE="/$userdir/history.$DT"...

Android中怎样自己制作su

本文原博客:http://hubingforever.blog.163.com/blog/static/171040579201372915716149/ 在Android源代码的systemextras(比方Android4.0systemextras)下新建一个文件夹。比方su_robin文件夹 在su_robin文件夹下包括以三个文件: su.h...

WebService 的Description 属性说明(转)

转自:http://exception.thinksaas.cn/0/173/173623.html  在WebMethod的description 中可使用超文本, 举例: 如上图中,红框类的WebService的接口说明是如何实现的?1、加粗字体的标题2、换行3、链接4、图标 这是服务接口http://www.webxml.com.cn/WebSer...

Azure Database for MySQL 多云容灾

        大家好,今天以此小文来说一说 Azure Database for MySQL 的多云容灾。作为任何一家云的 RDS PaaS 服务所有的可用性都是用白纸黑字的 SLA 支撑的,无论是4个9还是 n 个9 都意味着有 outage 的可能性, Everything fails all the time. 对于 RDS 数据库服务,如果服务实...

HTML5新特性之文字转语音

/** * 文字转语音,利用H5的新特性SpeechSynthesisUtterance,speechSynthesis实现 * eg. * const speaker = new Speaker({ text: '这是一条神奇的天路啊' }); * speaker.start(); // 开时播放 * setTimeout((...