dd/mm/yyyy vs dd/MM/yyyy?
Asked Answered
S

5

33
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

But when I do

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

result can be 23/04/2014, 23/05/2014, 23/06/2014 and so on with each run of program. Why so?

Schinica answered 23/3, 2014 at 11:40 Comment(3)
Have a look at the javadoc : docs.oracle.com/javase/7/docs/api/java/text/…Cordilleras
If you are particularly unlucky, results could also be 23/59/2014.Dainedainty
Update: These classes SimpleDateFormat and java.sql.Date have been supplanted by the modern java.time classes defined in JSR 310. Specifically replaced by DateTimeFormatter and LocalDate. See modern solution in Answer by Avinash.Jahncke
M
68

It's because mm is for minutes, not months. More in the documentation.


Just about the time you were asking your question the newer java.time package was added to the standard Java library. These days, you'd probably use that as shown in Arvind Kumar Avinash's answer. But the same thing applies to the formatter there, mm is minutes, MM is months.

Mease answered 23/3, 2014 at 11:40 Comment(0)
D
44

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.

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
Decanal answered 23/3, 2014 at 11:51 Comment(2)
That's really helpful!Waxler
Note that in some date/time libraries, D/DD is day-of-year while d/dd is day-of month.Lager
O
11

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

While MM represents months.

Read more about Time Patterns

Onetime answered 23/3, 2014 at 11:41 Comment(0)
W
3

Yes it is because mm represents 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

Thu Feb 20 14:33:00 IST 2020
20/02/2020
20/33/2020
Wayland answered 20/2, 2020 at 9:11 Comment(0)
T
3

java.time

The accepted answer is correct and also applicable to the modern date-time API. More in the documentation.

Note that Java-8, released in March 2014, introduced the modern date-time API which supplanted the outdated and error-prone java.util Date-Time API and their formatting API, SimpleDateFormat. Since then it has been strongly recommended to switch to the modern Date-Time API.

Demo using java.time, the modern Date-Time API:

public class Main {
    public static void main(String[] args) {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd/MM/uuuu", Locale.ENGLISH);
        System.out.println(LocalDate.now().format(formatter));

        // Alternatively,
        System.out.println(formatter.format(LocalDate.now()));
    }
}

Output:

19/07/2023
19/07/2023

ONLINE DEMO

Note: Here, you can use y instead of u but I prefer u to y.

Learn more about the modern Date-Time API from Trail: Date Time.

Tardiff answered 19/7, 2023 at 15:56 Comment(1)
Thanks for the good answer. While the same solution applies to java.time as applied to SimpleDateFormat in its days, the problem manifests quite differently. SimpleDateFormat gave nonsense output, which was awfully typical for that class. LocalDate will throw an exception because a date hasn’t got minutes and thus make sure that the problem is noticed and no nonsense output is produced. One of the many advantages of java.time.Amimia

© 2022 - 2024 — McMap. All rights reserved.