久久久久久久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;

現在我想用 yyyy-mm-dd 格式的日期對其進行初始化,我嘗試使用默認構造函數,但得到的輸出類似于 "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?

如何實現?

推薦答案

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

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 的當前時區設置(如上所示)還是想要控制您使用的時區.它會有所作為.

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.

最后,如果你真的堅持.正如你現在所理解的,我不能給你一個字符串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());
    }
}

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

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

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

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模板網!

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

相關文檔推薦

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:獲取當前星期幾的值)
主站蜘蛛池模板: 久久久久久久国产 | 91麻豆精品国产91久久久久久 | 99国产视频| 国产伦一区二区三区 | 久久毛片 | 国产在线一区观看 | 99久久久久| 欧美一级www片免费观看 | 欧美一区二 | 精品日韩在线观看 | 亚洲 欧美 精品 | 欧美精品91 | 91久久精品一区二区二区 | 超碰人人插 | 一本一道久久a久久精品蜜桃 | 偷拍自拍网| 精品一区二区久久久久久久网站 | 亚洲人成一区二区三区性色 | 一级在线免费观看 | 中文字幕av一区 | av片在线免费看 | 国产视频福利一区 | 日本成人在线网址 | 黄片毛片免费观看 | 国产在线观看 | 国际精品鲁一鲁一区二区小说 | 国产大片黄色 | 99视频在线 | 久久亚洲国产 | 成人在线播放网站 | 日本中文字幕一区 | 午夜免费电影 | 懂色av蜜桃av | 一区二区不卡 | 免费超碰| 日产精品久久久一区二区福利 | 97国产一区二区精品久久呦 | 中文字幕在线观看视频网站 | 亚洲综合激情 | 免费看91| 久久国际精品 |