balu balu - 1 month ago 8
Java Question

Convert Japan's date format (2013年11月24日) to normal date format (2013-11-24)

I have a date picker field on my JSP page. While selecting that field, the date is displayed in Japanese format (

2013年11月24日
) in my text field. Now, while reading that date field in my controller, I am getting this value
2013年11月24日
.

How can I convert this date format into normal date format?

Answer

It seems the format you've given is the default date format of the Japanese locale, so you can use the build in facility:

    DateFormat df = DateFormat.getDateInstance(DateFormat.FULL, new Locale("ja"));

Javadoc: http://docs.oracle.com/javase/7/docs/api/java/text/DateFormat.html

IDEONE example: http://ideone.com/0W7szq

DateFormat df = DateFormat.getDateInstance(DateFormat.FULL, new Locale("ja"));
System.out.println(df.format(new Date()));
System.out.println(df.parse("2013年11月24日"));

Output:

2013年11月24日

Sun Nov 24 00:00:00 GMT 2013

Edit:

Please note that this DateFormat class is not thread-safe, so you cannot make the instant static. If you do not want to create the instance again and again like above, you may want to look into the thread-safe variant in Joda time: DateTimeFormat.