草庐IT

java - 无法解析的希腊日期 - SimpleDateFormat

coder 2024-03-27 原文

我正在尝试使用 SimpleDateFormat 读取表示希腊日期时间的字符串(例如“28 Μαρτίου 2014, 14:00”),但它会抛出 java.text。 ParseException:无法解析的日期:“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) 首先要说的是,永远不要使用表达式 new Locale("el-GR"),而是使用 new Locale("el", "GR")或没有国家 new 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 for localized resources 中找到。现代希腊语至少知道三月的两种不同形式(Μαρτίου 与独立形式 Μìρτιος)。 Java 版本 6 使用独立形式,而 Java 版本 7 使用普通形式。

另见 compatibility note对于 java 版本 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, HH:mm", 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();
}

关于java - 无法解析的希腊日期 - SimpleDateFormat,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27939779/

有关java - 无法解析的希腊日期 - SimpleDateFormat的更多相关文章

  1. Ruby 解析字符串 - 2

    我有一个字符串input="maybe(thisis|thatwas)some((nice|ugly)(day|night)|(strange(weather|time)))"Ruby中解析该字符串的最佳方法是什么?我的意思是脚本应该能够像这样构建句子:maybethisissomeuglynightmaybethatwassomenicenightmaybethiswassomestrangetime等等,你明白了......我应该一个字符一个字符地读取字符串并构建一个带有堆栈的状态机来存储括号值以供以后计算,还是有更好的方法?也许为此目的准备了一个开箱即用的库?

  2. ruby-on-rails - 由于 "wkhtmltopdf",PDFKIT 显然无法正常工作 - 2

    我在从html页面生成PDF时遇到问题。我正在使用PDFkit。在安装它的过程中,我注意到我需要wkhtmltopdf。所以我也安装了它。我做了PDFkit的文档所说的一切......现在我在尝试加载PDF时遇到了这个错误。这里是错误:commandfailed:"/usr/local/bin/wkhtmltopdf""--margin-right""0.75in""--page-size""Letter""--margin-top""0.75in""--margin-bottom""0.75in""--encoding""UTF-8""--margin-left""0.75in""-

  3. ruby - 解析 RDFa、微数据等的最佳方式是什么,使用统一的模式/词汇(例如 schema.org)存储和显示信息 - 2

    我主要使用Ruby来执行此操作,但到目前为止我的攻击计划如下:使用gemsrdf、rdf-rdfa和rdf-microdata或mida来解析给定任何URI的数据。我认为最好映射到像schema.org这样的统一模式,例如使用这个yaml文件,它试图描述数据词汇表和opengraph到schema.org之间的转换:#SchemaXtoschema.orgconversion#data-vocabularyDV:name:namestreet-address:streetAddressregion:addressRegionlocality:addressLocalityphoto:i

  4. ruby - 用逗号、双引号和编码解析 csv - 2

    我正在使用ruby​​1.9解析以下带有MacRoman字符的csv文件#encoding:ISO-8859-1#csv_parse.csvName,main-dialogue"Marceu","Giveittohimóhe,hiswife."我做了以下解析。require'csv'input_string=File.read("../csv_parse.rb").force_encoding("ISO-8859-1").encode("UTF-8")#=>"Name,main-dialogue\r\n\"Marceu\",\"Giveittohim\x97he,hiswife.\"\

  5. ruby-on-rails - 无法使用 Rails 3.2 创建插件? - 2

    我对最新版本的Rails有疑问。我创建了一个新应用程序(railsnewMyProject),但我没有脚本/生成,只有脚本/rails,当我输入ruby./script/railsgeneratepluginmy_plugin"Couldnotfindgeneratorplugin.".你知道如何生成插件模板吗?没有这个命令可以创建插件吗?PS:我正在使用Rails3.2.1和ruby​​1.8.7[universal-darwin11.0] 最佳答案 随着Rails3.2.0的发布,插件生成器已经被移除。查看变更日志here.现在

  6. ruby - 无法运行 Rails 2.x 应用程序 - 2

    我尝试运行2.x应用程序。我使用rvm并为此应用程序设置其他版本的ruby​​:$rvmuseree-1.8.7-head我尝试运行服务器,然后出现很多错误:$script/serverNOTE:Gem.source_indexisdeprecated,useSpecification.Itwillberemovedonorafter2011-11-01.Gem.source_indexcalledfrom/Users/serg/rails_projects_terminal/work_proj/spohelp/config/../vendor/rails/railties/lib/r

  7. ruby-on-rails - 无法在centos上安装therubyracer(V8和GCC出错) - 2

    我正在尝试在我的centos服务器上安装therubyracer,但遇到了麻烦。$geminstalltherubyracerBuildingnativeextensions.Thiscouldtakeawhile...ERROR:Errorinstallingtherubyracer:ERROR:Failedtobuildgemnativeextension./usr/local/rvm/rubies/ruby-1.9.3-p125/bin/rubyextconf.rbcheckingformain()in-lpthread...yescheckingforv8.h...no***e

  8. ruby - 无法让 RSpec 工作—— 'require' : cannot load such file - 2

    我花了三天的时间用头撞墙,试图弄清楚为什么简单的“rake”不能通过我的规范文件。如果您遇到这种情况:任何文件夹路径中都不要有空格!。严重地。事实上,从现在开始,您命名的任何内容都没有空格。这是我的控制台输出:(在/Users/*****/Desktop/LearningRuby/learn_ruby)$rake/Users/*******/Desktop/LearningRuby/learn_ruby/00_hello/hello_spec.rb:116:in`require':cannotloadsuchfile--hello(LoadError) 最佳

  9. java - 等价于 Java 中的 Ruby Hash - 2

    我真的很习惯使用Ruby编写以下代码:my_hash={}my_hash['test']=1Java中对应的数据结构是什么? 最佳答案 HashMapmap=newHashMap();map.put("test",1);我假设? 关于java-等价于Java中的RubyHash,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/22737685/

  10. ruby-on-rails - date_field_tag,如何设置默认日期? [ rails 上的 ruby ] - 2

    我想设置一个默认日期,例如实际日期,我该如何设置?还有如何在组合框中设置默认值顺便问一下,date_field_tag和date_field之间有什么区别? 最佳答案 试试这个:将默认日期作为第二个参数传递。youcorrectlysetthedefaultvalueofcomboboxasshowninyourquestion. 关于ruby-on-rails-date_field_tag,如何设置默认日期?[rails上的ruby],我们在StackOverflow上找到一个类似的问

随机推荐