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

如何初始化公歷,日期為 YYYY-MM-DD 格式?

How to initialize Gregorian calendar with date as YYYY-MM-DD format?(如何初始化公歷,日期為 YYYY-MM-DD 格式?)
本文介紹了如何初始化公歷,日期為 YYYY-MM-DD 格式?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

限時送ChatGPT賬號..

我有一個包含變量的類

private GregorianCalendar cal;

現(xiàn)在我想用 yyyy-mm-dd 格式的日期對其進行初始化,我嘗試使用默認構(gòu)造函數(shù),但得到的輸出類似于 "2017-05-25T14:36:52+03:00",我只想要 "2017-05-25" 日期部分?

Now I want to initialize it with a date in yyyy-mm-dd format, I tried using the default constructor but the output I get is something like this "2017-05-25T14:36:52+03:00", I only want "2017-05-25" date part?

如何實現(xiàn)?

推薦答案

首先,您可能會討厭我的重復,您需要了解日期和該日期的字符串表示之間存在差異;基本數(shù)據(jù)和它的字符串表示之間的區(qū)別.

First, you may hate me for reiterating, you need to understand that there’s a difference between a date and a string representation of that date; a difference between the fundamental data and a string representation of it.

其次,如果您愿意,可以在 Java 7 中使用 LocalDate(以及其他 Java 日期和時間類).它(它們都在)在 ThreeTen-Backport ,將 Java SE 8 日期時間類向后移植到 Java SE 6 和 7.

Second, you can use LocalDate (and the other Java date and time classes) with Java 7 if you want. It is (and they are all) in the ThreeTen-Backport , a back-port of the Java SE 8 date-time classes to Java SE 6 and 7.

由于我收集到 LocalDate 更符合您的要求很多,我真的認為您應該考慮一下.所以我建議代替你的 cal

Since I gather that LocalDate fits your requirements much better, I really think you should give it a thought or two. So instead of your cal I suggest

private LocalDate date = LocalDate.now(ZoneId.systemDefault());

還要考慮您是想要 JVM 的當前時區(qū)設置(如上所示)還是想要控制您使用的時區(qū).它會有所作為.

Also think about whether you want the current time zone setting of your JVM (as the above will give you) or you want to control which time zone you use. It will make a difference.

最后,如果你真的堅持.正如你現(xiàn)在所理解的,我不能給你一個字符串in一個GregorianCalendar.您可以丟棄 GregorianCalendar 的時間部分,因此您只有日期部分.你可以把它格式化成你喜歡的字符串.

Finally, if you really insist. As you have understood by now, I cannot give you a string in a GregorianCalendar. You may discard the time part of your GregorianCalendar so you only have the date part. And you may format it into a string of your liking.

public class GregorianCalendarDemo {

    private GregorianCalendar cal;

    public GregorianCalendarDemo() {
        cal = new GregorianCalendar(TimeZone.getDefault(), Locale.getDefault());
        // discard time of day so we only have the date
        cal.set(Calendar.HOUR_OF_DAY, cal.getActualMinimum(Calendar.HOUR_OF_DAY));
        cal.set(Calendar.MINUTE, cal.getActualMinimum(Calendar.MINUTE));
        cal.set(Calendar.SECOND, cal.getActualMinimum(Calendar.SECOND));
        cal.set(Calendar.MILLISECOND, cal.getActualMinimum(Calendar.MILLISECOND));
    }

    protected GregorianCalendar getCal() {
        return cal;
    }

    public String getFormattedCal() {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        return format.format(getCal().getTime());
    }
}

當我剛剛調(diào)用 getFormattedCal() 時,它返回了 2017-05-25.

When I just called getFormattedCal(), it returned 2017-05-25.

再次,確定時區(qū)和語言環(huán)境的默認值是否合適,或者您想要其他值.

Again, decide whether default values for time zone and locale are fine or you want something else.

您可能認為我們可以只使用 cal.set(Calendar.HOUR_OF_DAY, 0): 來丟棄小時,同樣使用分鐘和秒.它至少適用于所有情況的 99%.但是,隨著夏令時(夏令時)的過渡,一天不能保證從 0 小時開始,所以上面的代碼更加安全.

You might have thought that we could discard the hours with just cal.set(Calendar.HOUR_OF_DAY, 0):, and similarly with minutes and seconds. It would work in 99 % of all cases at least. However, with transistion to summer time (daylight savings time), the day is not guaranteed to begin at 0 hours, so the above code is more bulletproof.

ThreeTen Backport 主頁

這篇關于如何初始化公歷,日期為 YYYY-MM-DD 格式?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

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

相關文檔推薦

Parsing an ISO 8601 string local date-time as if in UTC(解析 ISO 8601 字符串本地日期時間,就像在 UTC 中一樣)
How to convert Gregorian string to Gregorian Calendar?(如何將公歷字符串轉(zhuǎn)換為公歷?)
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 日之前日期的日歷到日期轉(zhuǎn)換.公歷到儒略歷切換)
java Calendar setFirstDayOfWeek not working(java日歷setFirstDayOfWeek不起作用)
Java: getting current Day of the Week value(Java:獲取當前星期幾的值)
主站蜘蛛池模板: 久久久精品网站 | 亚洲精品天堂 | 日韩欧美国产一区二区三区 | 国产精品美女久久久久久免费 | 亚洲精品乱码久久久久久蜜桃91 | 国产高清精品一区二区三区 | 成人欧美一区二区三区色青冈 | 四虎影视免费观看 | 亚洲人成人一区二区在线观看 | 一区二区在线 | 最新中文字幕第一页视频 | 91精品国产一区二区三区蜜臀 | 精品在线一区 | 久久久久久国产精品mv | 一区二区三区国产精品 | 天天天天操 | 精品视频在线观看 | 韩国欧洲一级毛片 | 欧美一级特黄aaa大片在线观看 | 亚洲一区国产精品 | 日韩av最新网址 | 在线免费观看a级片 | 欧美成人精品一区二区三区 | 日本久久久一区二区三区 | 操亚洲 | av久久 | 激情小说综合网 | 自拍视频精品 | 久久伊人一区 | 国产精品免费一区二区三区 | 国产一区二区三区不卡av | 欧美亚洲国产一区二区三区 | 中文字幕一区在线观看视频 | 在线伊人网 | 国产日韩欧美 | 久久国产电影 | 日韩精品一区二区三区视频播放 | 成年人精品视频在线观看 | 亚州国产 | www久久 | 日本亚洲欧美 |