簡體   English   中英

日/月/年與日/月/年?

[英]dd/mm/yyyy vs dd/MM/yyyy?

SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
        String date = sdf.format(new Date()); 
        System.out.println(date); 

結果是今天的日期,即 23/03/2014

但是當我這樣做時

 SimpleDateFormat sdf = new SimpleDateFormat("dd/mm/yyyy"); 

結果可以是 23/05/2014、23/05/2014、23/06/2014 和兒子,每次運行程序。 為什么這樣?

這是因為mm是幾分鍾,而不是幾個月。 文檔中的更多內容

在 C# 和 VB.NET 中,它是區分大小寫的,尤其是格式化月份或分鍾。 因為這兩個詞的首字母相同。

以下詳細信息可以幫助您格式化日期和時間。

1. d/D: day without 0 prefix on single digit. 
2. dd/DD: day with 0 prefix if single digit.
3. M: Month without 0 prefix on single digit.
4. MM: Month with 0 prefix if single digit.
5. yy/YY: Last two digit of year.
6. yyyy/YYYY: represents full year.
7. HH: 24 hour based hour. ie. 13:00. **H** will display hour without prefix 0 if single digit
8. hh: 12 hour based hour. ie. 01:00. **h** will display hour without prefix 0 if single digit
9. m: minute without 0 prefix on single digit. 
10.mm: minute with 0 prefix if single digit.
11.s: second without 0 prefix on single digit. 
12.ss: second with 0 prefix if single digit.
13.tt: represent the AM or PM

mm代表分鍾,所以當你使用mm ,它會打印分鍾而不是月份。

MM代表月份。

閱讀有關Time Patterns更多信息

是的,因為mm代表minutes

 Date date=new Date();
    System.out.println(date);
    System.out.println("Date: "+new SimpleDateFormat("dd/MM/yyyy").format(date));
    System.out.println("Date: "+new SimpleDateFormat("dd/mm/yyyy").format(date));

如果您觀察輸出,您會發現當我們打印日期對象時,有分鍾,月份等值,並且在第 2 行和第 3 行中,相同的分鍾對應於 mm 打印,月份值對應於 MM 打印

Thu Feb 20 14:33:00 IST 2020
20/02/2020
20/33/2020

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM