How can I set date and time formatting in Java that respects the user’s OS settings

First you have to tell Java what your system LOCALE looks like.

Check Java System.
String locale = System.getProperty("user.language")

And then format the date accordinly (SimpleDateFormat)
SimpleDateFormat(String pattern, Locale locale)

Refer to the practical Java code for a working example…

String systemLocale = System.getProperty("user.language");
String s;
Locale locale; 

locale = new Locale(systemLocale );
s = DateFormat.getDateInstance(DateFormat.MEDIUM, locale).format(new Date());
System.out.println(s);
// system locale is PT outputs 16/Jul/2011

locale = new Locale("us");
s = DateFormat.getDateInstance(DateFormat.MEDIUM, locale).format(new Date());
System.out.println(s);
// outputs Jul 16, 2011

locale = new Locale("fr");
s = DateFormat.getDateInstance(DateFormat.MEDIUM, locale).format(new Date());
System.out.println(s);
// outputs 16 juil. 2011  

Leave a Comment