1. 程式人生 > 其它 >Java——日期格式化YYYYMMdd與yyyyMMdd的區別

Java——日期格式化YYYYMMdd與yyyyMMdd的區別

技術標籤:Javajava

public static void main(String[] args) {
    //YYYY 是表示:當天所在的周屬於的年份,一週從週日開始,週六結束,只要本週跨年,那麼這周就算入下一年。
    //2019-12-29至2020-1-4跨年周
    Calendar calendar = Calendar.getInstance();
    //2019-12-28
    calendar.set(2019, Calendar.DECEMBER, 28);  
    Date strDate1 = calendar.getTime(); 
    //2019-12-29
    calendar.set(2019, Calendar.DECEMBER, 29);  
    Date strDate2 = calendar.getTime(); 
    // 2019-12-31  
    calendar.set(2019, Calendar.DECEMBER, 31);  
    Date strDate3 = calendar.getTime();  
    // 2020-01-01  
    calendar.set(2020, Calendar.JANUARY, 1);  
    Date strDate4 = calendar.getTime();  
        
    DateFormat df1 = new SimpleDateFormat("yyyyMMdd");
    DateFormat df2 = new SimpleDateFormat("YYYYMMdd");
    //yyyyMMdd
    System.out.println("yyyyMMdd");
    System.out.println("2019-12-28: " + df1.format(strDate1)); 
    System.out.println("2019-12-29: " + df1.format(strDate2)); 
    System.out.println("2019-12-31: " + df1.format(strDate3));  
    System.out.println("2020-01-01: " + df1.format(strDate4));  
    //YYYYMMdd
    System.out.println("YYYYMMdd");
    System.out.println("2019-12-28: " + df2.format(strDate1));
    System.out.println("2019-12-29: " + df2.format(strDate2));
    System.out.println("2019-12-31: " + df2.format(strDate3));  
    System.out.println("2020-01-01: " + df2.format(strDate4));
}

結果:
在這裡插入圖片描述
轉自:https://www.cnblogs.com/it-mh/p/12155400.html