Quick formatting of Date using a Java String format expression

Steve Neal Development 1 Comment

For quick reference, this is how you can format a date as:

dd/mm/yyyy at hh:mm:ss

System.out.printf("Date: %1$td/%1$tm/%1$tY at %1$tH:%1$tM:%1$tS%n", new Date());

The % characters mark a placeholders for String content, the 1$ indicates that the first vararg should be used for each placeholder (there is only one varag value supplied), the letter ‘t’ in each placeholder indicates a time conversion and the d, m, Y, H, M, S characters indicate what value should be taken from the new Date object for that time conversion.

Comments 1

Leave a Reply

Your email address will not be published. Required fields are marked *