Java 日/月/年与日/月/年?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22590381/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 16:42:47  来源:igfitidea点击:

dd/mm/yyyy vs dd/MM/yyyy?

javadate-format

提问by user3198603

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

Result is todays date i.e 23/03/2014

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

But when i do

但是当我这样做时

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

result can be 23/05/2014, 23/05/2014, 23/06/2014 and son with each run of prgram. Why so?

结果可以是 23/05/2014、23/05/2014、23/06/2014 和儿子,每次运行程序。为什么这样?

采纳答案by T.J. Crowder

As we can see from the documentation, it's because mmis for minutes, not months.

正如我们从文档中看到,这是因为mm是几分钟,而不是几个月。

回答by Salah

mmrepresents minutes, so when you use mm, it will print minutes instead of month.

mm代表分钟,所以当你使用时mm,它会打印分钟而不是月份。

While MMrepresents months.

whileMM代表月份。

Read more about Time Patterns

阅读更多关于 Time Patterns

回答by Shell

In C# and VB.NET, it's a case sensitive especially to format the Month or Minutes. Because initial character of both the words is same.

在 C# 和 VB.NET 中,它是区分大小写的,尤其是格式化月份或分钟。因为这两个词的首字母相同。

The following detail may help you in formatting the date & time.

以下详细信息可以帮助您格式化日期和时间。

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

回答by sanjeevjha

Yes it is because mmrepresents minutes

是的,因为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));

if you observed the output you will find when we print date object, there is value of minutes, month, etc and in 2nd line and 3rd line same minute is printed corresponding to mm and month value is printed corresponding to MM

如果您观察输出,您会发现当我们打印日期对象时,有分钟,月份等值,并且在第 2 行和第 3 行中,相同的分钟对应于 mm 打印,月份值对应于 MM 打印

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