I need to extract only the month and year information from a Java Date to display in a GUI. This date must be translated using the user locale. I know 2 ways for formatting localized dates:
DateFormat.getInstance(DateFormat.LONG, locale).format(date)
new SimpleDateFormat("yyyy M", locale).format(date)
For locale SIMPLIFIED_CHINESE
and date 2013-02-18
, this gives:
We need to use the LONG
format without day (2013年2月). [The variant 2 is clearly not acceptable since the ordering of year-month is not affected by the locale].
The problem could be: how to remove the day part (18日) from the long format ?
I tried using the method format(Date date, StringBuffer toAppendTo, FieldPosition pos)
, but this allows only extracting the number part, not the extra character.
The pattern of the DateFormat.getInstance(DateFormat.LONG, Locale.SIMPLIFIED_CHINESE)
is yyyy'年'M'月'd'日'
. Extracting the day part would yield yyyy'年'M'月'
.
It seems to me that it's not possible to use Java's DateFormat standard to achieve this since the extra characters (年
, 月
and 日
) do not seem mapped to the corresponding field, but are simply characters without other semantic for the formatter.
I had a look to DateFormatSymbols
and LocaleServiceProvider
, but think it doesn't help.
To me, the point of extension would be adding another date style to the getInstance(int style)
method. But it doesn't seem possible without implementing it for all the Locale...
Is this analysis correct ? How to achieve this formatting in a clean way ?
Java SimpleDateFormat Example String pattern = "MM-dd-yyyy"; SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern); String date = simpleDateFormat. format(new Date()); System. out. println(date);
DateValidator validator = new DateValidatorUsingDateFormat("MM/dd/yyyy"); assertTrue(validator. isValid("02/28/2019")); assertFalse(validator. isValid("02/30/2019")); This was the most common solution before Java 8.
Use the toLocaleString() method to get a date and time in the user's locale format, e.g. date. toLocaleString() . The toLocaleString method returns a string representing the given date according to language-specific conventions.
To format a date for the current Locale, use one of the static factory methods: myString = DateFormat. getDateInstance(). format(myDate);
I don't think that any pre-defined constants would exist for year-and-month formatting in zh-CN locale, so you have to format it.
new SimpleDateFormat("yyyy年M月").format(date)
As SimpleDateFormat
says (highlighted by me):
Date and time formats are specified by date and time pattern strings. Within date and time pattern strings, unquoted letters from 'A' to 'Z' and from 'a' to 'z' are interpreted as pattern letters representing the components of a date or time string. Text can be quoted using single quotes (') to avoid interpretation. "''" represents a single quote. All other characters are not interpreted; they're simply copied into the output string during formatting or matched against the input string during parsing.
So, it is safe to put chinese characters into the format string this way.
Since Java is Unicode, you can inline the characters in your source as long as you compile with unicode source character set. Otherwise you should read this String externally, e.g. from some Unicode property file or database.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With