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

設置 DAY_OF_WEEK 會返回意外結果

Setting DAY_OF_WEEK returns unexpected result(設置 DAY_OF_WEEK 會返回意外結果)
本文介紹了設置 DAY_OF_WEEK 會返回意外結果的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

限時送ChatGPT賬號..

我想將給定日歷實例的時間戳設置為一周的開始(星期一),而不是返回一個看似完全不相關的時間戳 - 除非我在此之前訪問任何日歷的字段.我在下面包含了一個示例,另請參閱 Ideone 中的這個可運行示例.

I want to set a given calendar instance's timestamp to the beginning of the week (Monday) and instead it returns a seemingly completely unrelated timestamp - unless I access any of the calendar's fields before doing so. I include a sample below, please also see this runnable example in Ideone.

這是預期的行為嗎?這背后的邏輯是什么?是的,我聽說過 Joda Time.

Is this expected behavior? What's the logic behind this? And yes, I've heard of Joda Time.

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Locale;
import java.util.TimeZone;


class MyTest {

private static Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone("CET"), Locale.FRANCE);
private static SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd");

public static void main(String[] args) {

    // Set to any date.
    calendar.set(2013, 10, 3);
    System.out.println(dateFormat.format(calendar.getTime()));

    // Set to another day.
    calendar.set(2014, 0, 15);
    // --- THE WTF STARTS HERE ---
    // Uncommenting the line below returns the correct date in the end.
    // calendar.getTime();

    // Set to monday of current week.
    calendar.set(Calendar.DAY_OF_WEEK, calendar.getFirstDayOfWeek());

    // Expected outdate is 20140113
    System.out.println(dateFormat.format(calendar.getTime()));

}

}

推薦答案

文檔中的字段操作章節解釋清楚.但它只是工作起來很奇怪.

Field Manipulation chapter in the docs explains it clearly. It just works weird though.

http://docs.oracle.com/javase/6/docs/api/java/util/Calendar.html

示例:考慮一個最初設置為 1999 年 8 月 31 日的 GregorianCalendar.調用set(Calendar.MONTH, Calendar.SEPTEMBER) 將日期設置為 1999 年 9 月 31 日.這是一個臨時的內部表示,解決到 1999 年 10 月 1 日,如果然后調用 getTime().但是,在之前調用 set(Calendar.DAY_OF_MONTH, 30)調用 getTime() 將日期設置為 1999 年 9 月 30 日,因為不會發生重新計算在 set() 本身之后.

Example: Consider a GregorianCalendar originally set to August 31, 1999. Calling set(Calendar.MONTH, Calendar.SEPTEMBER) sets the date to September 31, 1999. This is a temporary internal representation that resolves to October 1, 1999 if getTime()is then called. However, a call to set(Calendar.DAY_OF_MONTH, 30) before the call to getTime() sets the date to September 30, 1999, since no recomputation occurs after set() itself.

編輯

來自同一文檔的日歷字段解析部分

From the Calendar Fields Resolution part of the same doc

If there is any conflict in calendar field values, Calendar gives priorities to 
calendar fields that have been set more recently. The following are the default 
combinations of the calendar fields. The most recent combination, as determined 
by the most recently set single field, will be used.

For the date fields:

 YEAR + MONTH + DAY_OF_MONTH
 YEAR + MONTH + WEEK_OF_MONTH + DAY_OF_WEEK
 YEAR + MONTH + DAY_OF_WEEK_IN_MONTH + DAY_OF_WEEK
 YEAR + DAY_OF_YEAR
 YEAR + DAY_OF_WEEK + WEEK_OF_YEAR

我認為 MONTH 和 DAY_OF_WEEK 之間的區別是這樣的.如果您在最后一條語句中設置 MONTH,它將與 YEAR+MONTH+DAY_OF_MONTH 匹配并覆蓋所有這些.如果您設置 DAY_OF_WEEK 它與 YEAR+DAY_OF_WEEK+WEEK_OF_YEAR 匹配,因此不會覆蓋月份值.或類似的東西.老實說,我越看越覺得破碎.這根本沒有意義.最好繼續使用 JodaTime

I think the difference between MONTH and DAY_OF_WEEK is this. If you set MONTH at the last statement it matches with YEAR+MONTH+DAY_OF_MONTH and overrides all of them. If you set DAY_OF_WEEK it matches with YEAR+DAY_OF_WEEK+WEEK_OF_YEAR so doesn't override the month value. Or something like that. To be honest, the more I look the more broken it seems. It doesn't make sense at all. Better keep using JodaTime

這篇關于設置 DAY_OF_WEEK 會返回意外結果的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持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:獲取當前星期幾的值)
主站蜘蛛池模板: 国产日韩欧美二区 | 久久99精品视频 | 午夜网站视频 | 久久精品成人一区 | 久久精品在线 | 在线观看 亚洲 | 亚洲第一成年免费网站 | 激情自拍偷拍 | 黄色片在线免费看 | 一级爱爱片 | 亚洲精久久久 | 一级黄色录像毛片 | 黑人精品欧美一区二区蜜桃 | 国产高清一二三区 | 午夜精品久久久久久久 | 黄色毛片在线观看 | 成人在线视频网站 | 精品日韩一区 | 九色国产 | 国产精品久久久久久一区二区三区 | 亚洲 中文 欧美 日韩 在线观看 | 成人精品一区 | 黑人巨大精品欧美一区二区免费 | 国产精品精品久久久久久 | 精品国产乱码久久久久久闺蜜 | 日韩1区 | 神马久久香蕉 | 一区二区三区av | 羞羞视频免费在线 | 一区二区三区四区免费视频 | 天天干人人 | 国产一区2区 | www日本高清| 日韩免费福利视频 | 宅女噜噜66国产精品观看免费 | 色资源在线 | 亚州影院| av中文字幕在线 | 日本不卡一区二区 | 福利视频日韩 | 盗摄精品av一区二区三区 |