Java Language Automatically formatted Dates using "locale"


Example

SimpleDateFormatter is great in a pinch, but like the name suggests it doesn't scale well.

If you hard-code "MM/dd/yyyy" all over your application your international users won't be happy.

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);