如何使用不同的语言环境解析Java中的日期格式 [英] How can I parse Date format in Java with different locale
问题描述
我在使用英语操作系统的非英语语言环境中遇到Date.parse()方法的问题。
我正在使用Date.parse()方法来解析输入的日期值。不同的语言环境会有所不同。这里有些例子。
2016年2月3日00:00英语
2016年2月1日00:00德国
2016-2-01 00:00日语
I am facing issue with Date.parse() method for non-English locale with English OS. I am using the Date.parse() method to parse the entered value of date. It would be different for different locales. Here are some examples. 03-Feb-2016 00:00 English 01.Feb.2016 00:00 Germany 2016-2-01 00:00 Japanese
因此对于德语语言环境,在字符串(01.Feb.2016 00:00)上方传递给parse()
So for German locale, above string (01.Feb.2016 00:00) pass to the parse()
try
{
Date.parse( value );
}
catch( IllegalArgumentException e )
{
valid_value= false;
}
其中值为 01.Feb.2016 00:00,然后赶上块。
请帮助我如何验证值或转换英语语言环境可接受的值。
where value is "01.Feb.2016 00:00" then it goes to catch block. Please help me on how can I validate the value or convert the value acceptable by English locale.
推荐答案
您可以使用以下代码
//Germany local date
Date date = new Date();
DateFormat dateFormat;
dateFormat = DateFormat.getDateInstance(DateFormat.SHORT,Locale.GERMANY);
dateFormat.format(date);
OR
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd-MMM-yyyy", Locale.GERMANY);
simpleDateFormat.format(date);
这篇关于如何使用不同的语言环境解析Java中的日期格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!