Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to extract the date fields for locale-specific formatting in Java?

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:

  1. Using DateFormat.getInstance(DateFormat.LONG, locale).format(date)
  2. Using new SimpleDateFormat("yyyy M", locale).format(date)

For locale SIMPLIFIED_CHINESE and date 2013-02-18, this gives:

  1. 2013年2月18日
  2. 2013 2

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 ?

like image 806
Jidehem Avatar asked Feb 18 '13 09:02

Jidehem


People also ask

How do I get date in specified format?

Java SimpleDateFormat Example String pattern = "MM-dd-yyyy"; SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern); String date = simpleDateFormat. format(new Date()); System. out. println(date);

How do you check if a date is in a particular format in Java?

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.

How do I get a locale date?

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.

How will you format a date based on a locale after you have obtained a DateFormat object?

To format a date for the current Locale, use one of the static factory methods: myString = DateFormat. getDateInstance(). format(myDate);


1 Answers

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.

like image 58
gaborsch Avatar answered Oct 28 '22 00:10

gaborsch