Search code examples
javadate-format

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


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?


Solution

  • 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.