Automatically formatted Dates using locale
suggest changeSimpleDateFormat is fine for a fixed pattern, but a hard-coded pattern such as "MM/dd/yyyy" is wrong for many locales.
Let Java do the work for you
Use the static methods in DateFormat to retrieve the right formatting for your user. For a desktop application (where you’ll rely on the default locale), simply call:
String localizedDate = DateFormat.getDateInstance(style).format(date);
Where style is one of the formatting constants (FULL, LONG, MEDIUM, SHORT, etc.) specified in DateFormat.
For a server-side application where the user specifies their locale as part of the request, you should pass it explicitly to getDateInstance() instead:
String localizedDate =
DateFormat.getDateInstance(style, request.getLocale()).format(date);
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents