简体   繁体   中英

How to get a localized date format for EditText hint in android

How to get a localized date format for an EditText hint in android?

yyyy-mm-dd(EN)
aaaa-mm-dd(SP)
aaaa-mm-jj(FR)

You can use DateFormat.getDateFormat(Context) , which does the following:

Returns a DateFormat object that can format the date in short form (such as 12/31/1999) according to the current locale and the user's date-order preference.

-- DateFormat documentation

Then, pass to it an object of type Activity or Context :

java.text.DateFormat formatter = android.text.format.DateFormat.getDateFormat(context);

From SimpleDateFormat

SimpleDateFormat(String, Locale)

Constructs a new SimpleDateFormat using the specified non-localized pattern and the DateFormatSymbols and Calendar for the specified locale.

So the pattern should not change

DateFormatSymbols

Encapsulates localized date-time formatting data, such as the names of the months, the names of the days of the week, and the time zone data. DateFormat and SimpleDateFormat both use DateFormatSymbols to encapsulate this information.

The only thing that should change is month name, tzdata or day of week name if you'd used any.

For getting a localized pattern String use

toLocalizedPattern()

http://developer.android.com/reference/java/text/SimpleDateFormat.html#toLocalizedPattern%28%29

在调用SimpleDateFormat.toLocalizedPattern()之前尝试调用SimpleDateFormat.applyLocalizedPattern(String template) SimpleDateFormat.toLocalizedPattern()

you can do something like this:

DateFormat dateFormat = android.text.format.DateFormat.getDateFormat(mContext);

String pattern = ((SimpleDateFormat) dateFormat).toLocalizedPattern();

editText.setHint(pattern);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM