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?
Are the delimiters always the same? If so, can't you just use
SimpleDateFormat("yyyy年MM月dd")
?It seems the format you've given is the default date format of the Japanese locale, so you can use the build in facility:
Javadoc: http://docs.oracle.com/javase/7/docs/api/java/text/DateFormat.html
IDEONE example: http://ideone.com/0W7szq
Output:
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.
The Answer by billc.cn is correct but outdated. The troublesome old date-time classes are now legacy, supplanted by the java.time classes.
java.time
See live code in IdeOne.com.
LocalDate
The
LocalDate
class represents a date-only value without time-of-day and without time zone.A time zone is crucial in determining a date. For any given moment, the date varies around the globe by zone. For example, a few minutes after midnight in Paris France is a new day while still “yesterday” in Montréal Québec.
You should be using
LocalDate
objects to hold your date-only values in your business logic and data model. Generate the strings only as needed for presentation such as display in your JSP page.About java.time
The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as
java.util.Date
,Calendar
, &SimpleDateFormat
.The Joda-Time project, now in maintenance mode, advises migration to java.time.
To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.
Where to obtain the java.time classes?
The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as
Interval
,YearWeek
,YearQuarter
, and more.