Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to display date format according to locale?

I want to display a hint how a date should be entered. For Germany it might look like

"Enter date in this format: dd.mm.yyy"

For the US

"Enter date in this format: mm/dd/yyyy"

I understand that with

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

I can convert a date into a format matching the user's locale. But how would I get the day/month/year string for the hint?

And of course I would want the similar thing with a hint for time, too.

Thanks for any suggestions.

like image 508
Addi Avatar asked Mar 19 '15 11:03

Addi


People also ask

How do I format a time of day according to locale?

For formatting a time of day according to a user’s locale, get a formatter from DateTimeFormatter.ofLocalizedTime. For both date and time together use one of the overloaded versions of DateTimeFormatter.ofLocalizedDateTime. I recommend you don’t use DateFormat, SimpleDateFormat and Date.

Should I reset the default locale after formatting the date and time?

Also note that if you are not doing anything else locale specific in the rest of your program, you should reset the locale back to default after you are done with formatting the date and time. Otherwise, you might get some unexpected surprises when outputting or parsing numbers.

How to display date according to country locale format in angular?

To display date according to country locale format rules, We have to pass country locale code as a third parameter to angular date pipe as shown below. For example France follows Central European Summer Time and it has an timezone offset ‘+0200’.

How to localize date and time in JavaScript?

Another important date and time localization method that you should understand is that using the locale. Actually, the locale is a framework to switch between multiple languages. Take a look at the below code snippet for a better idea of this: d1= time.strftime("%A, %d.


1 Answers

You can use the getBestDateTimePattern method of DateFormat:

String datePattern = DateFormat.getBestDateTimePattern(Locale.getDefault(), "ddMMyyyy");
String timePattern = DateFormat.getBestDateTimePattern(Locale.getDefault(), "HHmmss");

note: assumes 24-hour format for the time. You might want to convert to all upper or lower case before presentation to the user.

like image 69
samgak Avatar answered Sep 28 '22 16:09

samgak