I'm trying to format a date in Java in different ways based on the given locale. For instance I want English users to see "Nov 1, 2009" (formatted by "MMM d, yyyy") and Norwegian users to see "1. nov. 2009" ("d. MMM. yyyy").
The month part works OK if I add the locale to the SimpleDateFormat constructor, but what about the rest?
I was hoping I could add format strings paired with locales to SimpleDateFormat, but I can't find any way to do this. Is it possible or do I need to let my code check the locale and add the corresponding format string?
tl;dr
The troublesome classes of
java.util.Date
andSimpleDateFormat
are now legacy, supplanted by the java.time classes.LocalDate
The
LocalDate
class represents a date-only value without time-of-day and without time zone.A time zone is crucial in determining a date. For any given moment, the date varies around the globe by zone. For example, a few minutes after midnight in Paris France is a new day while still “yesterday” in Montréal Québec.
DateTimeFormatter
Use
DateTimeFormatter
to generate strings representing only the date-portion or the time-portion.The
DateTimeFormatter
class can automatically localize.To localize, specify:
FormatStyle
to determine how long or abbreviated should the string be.Locale
to determine (a) the human language for translation of name of day, name of month, and such, and (b) the cultural norms deciding issues of abbreviation, capitalization, punctuation, and such.Example:
Going the other direction, you can parse a localized string.
Note that the locale and time zone are completely orthogonal issues. You can have a Montréal moment presented in Japanese language or an Auckland New Zealand moment presented in Hindi language.
Another example: Change
6 junio 2012
(Spanish) to2012-06-06
(standard ISO 8601 format). The java.time classes use ISO 8601 formats by default for parsing/generating strings.Peruse formats
Here is some example code for perusing the results of multiple formats in multiple locales, automatically localized.
An
EnumSet
is an implementation ofSet
, highly optimized for both low memory usage and fast execution speed when collectingEnum
objects. So,EnumSet.allOf( FormatStyle.class )
gives us a collection of all four of theFormatStyle
enum objects to loop. For more info, see Oracle Tutorial on enum types.Output.
About java.time
The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as
java.util.Date
,Calendar
, &SimpleDateFormat
.The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.
To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.
You may exchange java.time objects directly with your database. Use a JDBC driver compliant with JDBC 4.2 or later. No need for strings, no need for
java.sql.*
classes.Where to obtain the java.time classes?
The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as
Interval
,YearWeek
,YearQuarter
, and more.Use the style + locale: DateFormat.getDateInstance(int style, Locale locale)
Check http://java.sun.com/j2se/1.5.0/docs/api/java/text/DateFormat.html
Run the following example to see the differences:
Output:
Java8
Use DateFormat.getDateInstance(int style, Locale locale) instead of creating your own patterns with
SimpleDateFormat
.Localization of date string:
Based on redsonic's post:
will be like 05-九月-2013