加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

java – Unparseable希腊语日期 – SimpleDateFormat

发布时间:2020-12-14 16:46:24 所属栏目:Java 来源:网络整理
导读:我正在尝试使用SimpleDateFormat读取表示希腊语日期时间的字符串(如“28Μαρτ?ου2014,14:00”),但它会抛出 java.text.ParseException:Unparseable date:“28Μαρτ?ου2014,14:00 “错误. 这是一个示例代码: Locale locale = new Locale("el-GR
我正在尝试使用SimpleDateFormat读取表示希腊语日期时间的字符串(如“28Μαρτ?ου2014,14:00”),但它会抛出 java.text.ParseException:Unparseable date:“28Μαρτ?ου2014,14:00 “错误.

这是一个示例代码:

Locale locale = new Locale("el-GR");
SimpleDateFormat formatter = new SimpleDateFormat("dd MMMM yyyy,HH:mm",locale);
try {
     sDate = (Date) formatter.parse("28 Μαρτ?ου 2014,14:00");
 } catch (ParseException ex) {
     ex.printStackTrace();
 }

我也试过locales el和el_GR,但没有运气.

有什么建议?

解决方法

a)首先要说的是,永远不要使用表达式新的Locale(“el-GR”),而是使用新的Locale(“el”,“GR”)或没有国家/地区新的Locale(“el”),请参阅javadoc以正确使用构造函数(因为没有语言代码“el-GR”).

b)您观察到的异常(以及我,但不是每个人)都是由底层JVM的不同本地化资源引起的.我的JVM证明(1.6.0_31):

Locale locale = new Locale("el");
DateFormatSymbols dfs = DateFormatSymbols.getInstance(locale);
for (String m : dfs.getMonths()) {
    System.out.println(m);
}

// output
Μ?ρτιο?
Απρ?λιο?
Μ??ο?
Ιο?νιο?
Ιο?λιο?
Α?γουστο?
Σεπτ?μβριο?
Οκτ?βριο?
Νο?μβριο?
Δεκ?μβριο?

可以在CLDR-repository中找到有关不同数据的说明,以获取本地化资源.现代希腊语知道3月份的至少两种不同形式(Μαρτ?ουvs独立形式Μ?ρτιο?). Java-version 6使用独立形式,而Java-version 7使用普通形式.

另请参阅此compatibility note for java-version 8,其中您可以选择指定格式模式(独立或不单独):

When formatting date-time values using DateFormat and
SimpleDateFormat,context sensitive month names are supported for
languages that have the formatting and standalone forms of month
names. For example,the preferred month name for January in the Czech
language is ledna in the formatting form,while it is leden in the
standalone form. The getMonthNames and getShortMonthNames methods of
DateFormatSymbols return month names in the formatting form for those
languages. Note that the month names returned by DateFormatSymbols
were in the standalone form until Java SE 7
. You can specify the
formatting and/or standalone forms with the Calendar.getDisplayName
and Calendar.getDisplayNames methods…

所以显而易见的解决方案是更新到Java 7.外部库在这里没有帮助,因为今天没有一个人拥有自己的希腊语资源.但是,如果您因任何原因被迫继续使用Java 6,那么遵循笨拙的解决方法将有助于:

Locale locale = new Locale("el","GR");
SimpleDateFormat formatter = new SimpleDateFormat("dd MMMM yyyy,locale);
DateFormatSymbols dfs = DateFormatSymbols.getInstance(locale);
String[] months = {"Ιανουαρ?ου","Φεβρουαρ?ου","Μαρτ?ου","Απριλ?ου","Μα?ου","Ιουν?ου","Ιουλ?ου","Αυγο?στου","Σεπτεμβρ?ου","Οκτωβρ?ου","Νοεμβρ?ου","Δεκεμβρ?ου"};
dfs.setMonths(months);
formatter.setDateFormatSymbols(dfs);

try {
     System.out.println(formatter.parse("28 Μαρτ?ου 2014,14:00"));
     // output in my timezone: Fri Mar 28 14:00:00 CET 2014
} catch (ParseException ex) {
     ex.printStackTrace();
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读