久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

2 月 java.util.Calendar 的一個奇怪行為

A strange behavior from java.util.Calendar on February(2 月 java.util.Calendar 的一個奇怪行為)
本文介紹了2 月 java.util.Calendar 的一個奇怪行為的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

限時送ChatGPT賬號..

我遇到了來自 java.util.Calendar 的奇怪行為:

I faced with a strange behavior from java.util.Calendar:

import static org.junit.Assert.*;
import org.junit.Test;

import java.util.Calendar;

public class Tester1 {
    @Test
    public void test_monthOfDate() {
        assertEquals(1, monthOfDate(2013, 1, 30)); // OK
        assertEquals(1, monthOfDate(2013, 1, 31)); // OK

        // Start of February
        assertEquals(2, monthOfDate(2013, 2, 1));  // FAIL
        assertEquals(2, monthOfDate(2013, 2, 28)); // FAIL
        // to the end of it

        // and after that it is okay also
        assertEquals(3, monthOfDate(2013, 3, 1));  // OK
    }

    public int monthOfDate(int year, int month, int day) {
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.YEAR, year);
        cal.set(Calendar.MONTH, month - 1);

        // just a simple get! but seems it is very important
        cal.get(Calendar.MONTH);
        //

        cal.set(Calendar.DAY_OF_MONTH, day);

        return cal.get(Calendar.MONTH) + 1;
    }
}

我想知道為什么會這樣?

I want to know why exactly this is happening?

推薦答案

問題是您從 2013 年 1 月 30 日開始的日歷.

The problem is that you're starting off with a calendar on January 30th 2013.

然后您將年份設置為 2013 年 - 這不是問題.

You're then setting the year to 2013 - that's not a problem.

然后您將月份設置為 1(即二月).你期望在這里發生什么?實際發生的是它會記住它需要將月份設置為 1,但 不會 重新計算實際時間值.根據 文檔(重點是我的):

You're then setting the month to 1 (i.e. February). What do you expect to happen here? What actually happens is that it will remember that it needs to set the month to 1, but not recompute the actual time value. The time value will be recomputed on your call to get though, as per the documentation (emphsis mine):

set(f, value) 將日歷字段 f 更改為 value.此外,它設置一個內部成員變量來指示日歷字段 f 已更改.盡管日歷字段 f 立即更改,但在下一次調用 get()、getTime()、getTimeInMillis()、add() 或 roll() 之前,不會重新計算日歷的時間值(以毫秒為單位).因此,多次調用 set() 不會觸發多次不必要的計算.作為使用 set() 更改日歷字段的結果,其他日歷字段也可能更改,具體取決于日歷字段、日歷字段值和日歷系統.另外,get(f)在重新計算日歷字段后,不一定會返回調用set方法設置的值.具體由具體的日歷類決定.

set(f, value) changes calendar field f to value. In addition, it sets an internal member variable to indicate that calendar field f has been changed. Although calendar field f is changed immediately, the calendar's time value in milliseconds is not recomputed until the next call to get(), getTime(), getTimeInMillis(), add(), or roll() is made. Thus, multiple calls to set() do not trigger multiple, unnecessary computations. As a result of changing a calendar field using set(), other calendar fields may also change, depending on the calendar field, the calendar field value, and the calendar system. In addition, get(f) will not necessarily return value set by the call to the set method after the calendar fields have been recomputed. The specifics are determined by the concrete calendar class.

當您嘗試將1 月 30 日"更改為2 月 30 日"并強制進行計算時,實際上會發生的是您在 3 月 2 日結束了我的盒子 - 但您的可能會有所不同實施.

When you try to change "January 30th" to "February 30th" and force a computation, what actually happens is that you end up on March 2nd on my box - but it may differ on your implementation.

最好的修復方法是:

  • 使用 Calendar.set(year, month, date) 來避免這個排序問題
  • 首先使用 Joda Time 作為更合理的 API.
  • Use Calendar.set(year, month, date) instead to avoid this ordering issue
  • Use Joda Time as a more sensible API in the first place.

這篇關于2 月 java.util.Calendar 的一個奇怪行為的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

Parsing an ISO 8601 string local date-time as if in UTC(解析 ISO 8601 字符串本地日期時間,就像在 UTC 中一樣)
How to convert Gregorian string to Gregorian Calendar?(如何將公歷字符串轉換為公歷?)
Java: What/where are the maximum and minimum values of a GregorianCalendar?(Java:GregorianCalendar 的最大值和最小值是什么/在哪里?)
Calendar to Date conversion for dates before 15 Oct 1582. Gregorian to Julian calendar switch(1582 年 10 月 15 日之前日期的日歷到日期轉換.公歷到儒略歷切換)
java Calendar setFirstDayOfWeek not working(java日歷setFirstDayOfWeek不起作用)
Java: getting current Day of the Week value(Java:獲取當前星期幾的值)
主站蜘蛛池模板: 亚洲欧美在线视频 | 免费看片在线播放 | 国产精品一区二区三区四区五区 | 亚欧洲精品在线视频免费观看 | 亚洲电影第1页 | 日日操夜夜摸 | 国产女人第一次做爰毛片 | 男女黄网站 | 在线观看的av | 一级毛片色一级 | 亚洲高清视频一区二区 | 99爱在线视频 | 久久免费精品 | 亚洲中午字幕 | 天堂色 | 欧美日韩久久久 | 欧美日韩三区 | 亚洲欧美日韩高清 | 中文字幕免费在线 | 天天影视综合 | 日韩av一区二区在线观看 | 国产精品久久久久久吹潮 | 亚洲成人一级 | 黄瓜av| 欧美mv日韩mv国产网站91进入 | 国产在线视频一区 | 日韩三级精品 | 老司机成人在线 | 亚洲精品一区二区三区在线 | 国产乱码精品一品二品 | 亚洲精品久久久久中文字幕欢迎你 | 国产一区二区在线视频 | 日韩精品视频一区二区三区 | 久久九九免费 | 日韩一二三区 | 国产亚洲一区二区三区 | 一区二区三区国产 | 羞羞视频免费在线 | 欧美8一10sex性hd | 欧美日韩在线一区二区 | 国产视频一区在线 |