zoukankan      html  css  js  c++  java
  • Calender设置固定时间遇到的问题

    在使用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

  • 相关阅读:
    [Bzoj3262]陌上花开(CDQ分治&&树状数组||树套树)
    [洛谷P1501][国家集训队]Tree II(LCT)
    [bzoj2002][Hnoi2010]Bounce 弹飞绵羊(LCT)
    Codeforces Round #683 (Div. 2, by Meet IT) E
    Codeforces Round #683 (Div. 2, by Meet IT) C
    set使用
    Educational Codeforces Round 98 (Rated for Div. 2) D
    Educational Codeforces Round 98 (Rated for Div. 2) B
    arc102a
    树状数组知识点整理二(待)
  • 原文地址:https://www.cnblogs.com/sonofelice/p/6237702.html
Copyright © 2011-2022 走看看