Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I format date by locale in Java? [duplicate]

I need to format date to app that has many languages, what is best way to format date, because every country has different kind of date formatting, so is it possible to format date by locale?

like image 940
newbie Avatar asked Dec 23 '09 10:12

newbie


People also ask

How do you localize a date in Java?

Java SimpleDateFormat with Locale String pattern = "EEEEE MMMMM yyyy HH:mm:ss. SSSZ"; SimpleDateFormat simpleDateFormat =new SimpleDateFormat(pattern, new Locale("fr", "FR")); String date = simpleDateFormat. format(new Date()); System.

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);

How do you change date format to MM DD YYYY in Java?

You can just use: Date yourDate = new Date(); SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd"); String date = DATE_FORMAT. format(yourDate);


2 Answers

Yes, using DateFormat.getDateInstance(int style, Locale aLocale) This displays the current date in a locale-specific way.

So, for example:

DateFormat df = DateFormat.getDateInstance(DateFormat.SHORT, yourLocale); String formattedDate = df.format(yourDate); 

See the docs for the exact meaning of the style parameter (SHORT, MEDIUM, etc)

like image 134
Bozho Avatar answered Sep 24 '22 17:09

Bozho


SimpleDateFormat has a constructor which takes the locale, have you tried that?

http://java.sun.com/javase/6/docs/api/java/text/SimpleDateFormat.html

Something like new SimpleDateFormat("your-pattern-here", Locale.getDefault());

like image 28
laura Avatar answered Sep 21 '22 17:09

laura