将日本的日期格式(2013年11月24日)转换为正常的日期格式(2013-11-24) [英] Convert Japan's date format (2013年11月24日) to normal date format (2013-11-24)

查看:472
本文介绍了将日本的日期格式(2013年11月24日)转换为正常的日期格式(2013-11-24)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的JSP页面上有一个日期选择器字段.选择该字段时,日期在我的文本字段中以日语格式(2013年11月24日)显示.现在,在我的控制器中读取该日期字段时,我正在获取该值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?

推荐答案

您提供的格式似乎是日语语言环境的默认日期格式,因此您可以使用内置工具:

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示例: http://ideone.com/0W7szq

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

输出:

2013年11月24日

2013年11月24日

2013年11月24日星期天格林尼治标准时间00:00:00

Sun Nov 24 00:00:00 GMT 2013

请注意,此DateFormat类不是线程安全的,因此不能使即时静态.如果您不想像上述那样一次又一次地创建实例,则可能需要研究Joda time中的线程安全变体:

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.

这篇关于将日本的日期格式(2013年11月24日)转换为正常的日期格式(2013-11-24)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆