How does the JAVA date format 06/06/2011 use GregorianCalendar instead of 04/06/2011? String.format ("% 1 $ tm /% 1 $ td /% 1 $ tY", today);

Like JAVA Date Format

06/06/2011

instead of 0

06/06/2011

?

I need to use GregorianCalendar

GregorianCalendar today=new GregorianCalendar(); String todayStr = String.format("%1$tm/%1$td/%1$tY", today); 

gives

06/04/2011

I want to

06/06/2011

early

+4
source share
2 answers
 DateFormat dateFormat1=new SimpleDateFormat("M/dd/yyyy"); System.out.println(dateFormat1.format(new Date())); 

Output:

06/06/2011

Note: This may be unsuccessful for a month longer than October.

+6
source

You cannot do this using String.format(...) . See the documentation, there is no such converter as a month without leading zeros. You can either use DateFormat , or do it somehow manually (separate the leading zero from the month, if any).

0
source

Source: https://habr.com/ru/post/1346874/


All Articles