Change date template from shortDate () format to JodaTime

I have a LocalDate, and I would like to print the date with day and month as 2 digits, and the year 4 digits, based on the Locale format template. Actually, I can use DateTimeFormatter to set a short date, but unfortunately, “I cannot” change the default template ... Is there a way to do this?

Below I explain this with an example:

Locale l = Locale.ITALIAN;
LocalDate localDate = new LocalDate( 2014, 8, 28);
DateTimeFormatter formatterOutput1 = DateTimeFormat.shortDate().withLocale(l);
System.out.println( "Output1: " + formatterOutput1.print(localDate));

l = Locale.ENGLISH;
DateTimeFormatter formatterOutput2 = DateTimeFormat.shortDate().withLocale(l);
System.out.println( "Output2: " + formatterOutput2.print(localDate));

This is the result:

Output1: 28/08/14
Output2: 8/28/14

So, the conclusion is correct, but I expect this:

Output1: 28/08/2014
Output2: 08/28/2014

How to change the default template?

Thank!

+4
source share
2 answers

shortDate() forStyle() S -

DateTimeFormat.patternForStyle("S-", Locale.ITALIAN);

String Pattern, yy yyyy

:

LocalDate localDate = new LocalDate(2014, 8, 28);
String pattern = DateTimeFormat.patternForStyle("S-", Locale.ITALIAN);
pattern = pattern.replace("yy", "yyyy");
DateTimeFormatter formatterOutput1 = DateTimeFormat.forPattern(pattern);
System.out.println("Output1: " + formatterOutput1.print(localDate));

:

Output1: 28/08/2014

ENGLISH Locale:

String patternEnglish = DateTimeFormat.patternForStyle("S-", Locale.ENGLISH);
patternEnglish = patternEnglish.replace("yy", "yyyy");
DateTimeFormatter formatterOutput2 = DateTimeFormat.forPattern(patternEnglish);
System.out.println("Output2: " + formatterOutput2.print(localDate));

:

Output2: 8/28/2014

, , Locale, .

+5

. :

DateTimeFormatter formatterOutput1 = DateTimeFormat.forPattern("dd/MM/yyyy");
DateTimeFormatter formatterOutput2 = DateTimeFormat.forPattern("MM/dd/yyyy");
+2

All Articles