久久久久久久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:獲取當前星期幾的值)
主站蜘蛛池模板: 国产精品视频一二三区 | 欧美videosex性极品hd | 日韩欧美在线视频 | 国产精品特级毛片一区二区三区 | 综合婷婷 | 欧美日韩中文字幕在线 | 日韩中文在线视频 | 久久久网 | 亚洲美女一区二区三区 | 久久精品小视频 | 亚洲综合大片69999 | 国产一区二区三区在线免费观看 | 久久神马 | 女同videos另类 | 久久久国产一区 | 亚洲视频一区 | 亚洲精品黄色 | 成人在线精品视频 | 精品伊人久久 | 欧美日韩一区二区三区四区 | 精品国产91亚洲一区二区三区www | 91精品国产91久久久久久密臀 | 亚洲成a人片| 在线免费看黄 | 欧美日韩国产一区二区三区不卡 | 欧洲精品码一区二区三区免费看 | 亚洲欧美一区二区三区视频 | 精品少妇一区二区三区日产乱码 | 国产成人jvid在线播放 | 91麻豆精品一区二区三区 | 国产一区二区三区四区 | 欧美日韩专区 | 日本久久视频 | 国产精品成人一区二区三区夜夜夜 | 一区二区三区亚洲 | 亚洲精品片 | 水蜜桃亚洲一二三四在线 | 亚洲一区二区三区 | 香蕉视频久久久 | 免费观看一级特黄欧美大片 | 亚洲高清久久 |