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?
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.
Are the delimiters always the same?
If so, can't you just use SimpleDateFormat("yyyy年MM月dd")
?
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
DateTimeFormatter f = DateTimeFormatter.ofLocalizedDate( FormatStyle.FULL );
f = f.withLocale( Locale.forLanguageTag("ja") ) ;
String input = "2013年11月24日" ;
LocalDate ld = LocalDate.parse( input , f );
input: 2013年11月24日
ld.toString(): 2013-11-24
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.
ZoneId z = ZoneId.of( "America/Montreal" );
LocalDate today = LocalDate.now( z );
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?
- Java SE 8 and SE 9 and later
- Built-in.
- Part of the standard Java API with a bundled implementation.
- Java 9 adds some minor features and fixes.
- Java SE 6 and SE 7
- Much of the java.time functionality is back-ported to Java 6 & 7 in ThreeTen-Backport.
- Android
- The ThreeTenABP project adapts ThreeTen-Backport (mentioned above) for Android specifically.
- See How to use….
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.