用Java解析法语日期 [英] Parsing french dates in Java

查看:361
本文介绍了用Java解析法语日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我获得以下日期字符串

10 juil 2014

用法语查看一年中的月份名称,我看到 juil 是...的缩写 juillet ,这是指七月的英文。

Looking up the name of the months of the year in French, I see that juil is an abbreviation for juillet, which refers to July in English.

我尝试使用 SimpleDateFormat解析它 法语区域设置:

I try to parse it using SimpleDateFormat with French locale:

System.out.println(new SimpleDateFormat("dd MMM yyyy", Locale.FRENCH).parse("11 juil 2014"));

但它抛出异常

java.text.ParseException: Unparseable date: "11 juil 2014"
    at java.text.DateFormat.parse(DateFormat.java:357)

然后我尝试在月份名称后添加句号

I then try adding a period right after the month name

System.out.println(new SimpleDateFormat("dd MMM yyyy", Locale.FRENCH).parse("11 juil. 2014"));

现在我得到以下输出

Fri Jul 11 00:00:00 EDT 2014

所以看起来我需要一个句号,但是当我尝试解析三月日期( mars )时,如果你添加句号,则无法识别。

So it looks like I need a period, but then when I try to parse a March date (mars), if you add the period, it is not recognized.

我应该如何解析法语日期?我可以通过两次传递:第一次有一段时间,然后没有一段时间,并希望其中一个能做到这一点,但有更好的方法吗?

How should I parse french dates? I can do it in two passes: first with a period, and then without a period, and hope that one of them will do the trick, but is there a better way?

推荐答案

在法语中,缩写的月份名称有一个句号。

In French, abbreviated month names have a period.

请参阅耶鲁大学图书馆的这页,月份名称的缩写。列出几十种语言。

See this page at Yale University Library, Abbreviations of the Names of the Months. Lists a few dozen languages.

mars是March的全名(四个字母)。这个名字很短,不需要缩写。没有缩写,所以没有句号。同样适用于mai(五月),juin(六月)和août(八月)。

"mars" is the full name for March (four letters). That name is so short as to not require abbreviating. No abbreviation, so no period. Same for "mai" (May), "juin" (June), and août (August).

另外,您可能已经注意到,第一个字母是小写的用法语,但用英文大写。

Also, as you may have noticed, the first letter is lowercase in French but uppercase in English.

我在 Joda-Time 2.4在Mac OS X Mountain Lion上的Java 8中。 [跳转到java.time,Joda-Time的替换]

I tried this in Joda-Time 2.4 in Java 8 on Mac OS X Mountain Lion. [Jump down for java.time, Joda-Time’s replacement]

LocalDate localDate = DateTimeFormat.forPattern( "dd MMM yyyy" ).withLocale( java.util.Locale.FRENCH ).parseLocalDate( "10 juil 2014" );



同样的问题:缺少期限



两者 juillet juil。成功解析为法语,但 juil 失败并抛出异常。月份缩写预计会有句号终结符。

Same Problem: Missing Period

Both juillet and juil. successfully parse as French, but juil fails and throws an exception. The month abbreviation is expected to have a period terminator.

让我们使用 substring lastIndexOf 拆除字符串,添加句点,然后重建字符串。

Let's use substring and lastIndexOf to tear apart the string, add a period, and rebuild the string.

测试字符串是否包含:janv,févr,avr,juil,sept,oct,nov,déc。如果你得到一个包含完整月份名称而不是缩写的字符串,请注意使用双方空格。

Test if the string contains: " janv ", " févr ", " avr ", " juil ", " sept ", " oct ", " nov ", " déc ". Note the use of spaces of both sides in case you get a string with the full month name rather than abbreviation.

String inputRaw = "10 juil 2014";
int indexOfSecondSpace = inputRaw.lastIndexOf( " " );
String input = inputRaw.substring( 0, indexOfSecondSpace ) + "." + inputRaw.substring( indexOfSecondSpace );
DateTimeFormatter formatter = DateTimeFormat.forPattern( "dd MMM yyyy" ).withLocale( java.util.Locale.FRENCH );
LocalDate localDate = formatter.parseLocalDate( input );

System.out.println( inputRaw + " → " + input + " → " + localDate );

运行时。

10 juil 2014 → 10 juil. 2014 → 2014-07-10

或致电 replace 替换:

Or call replace to do a replacement of:


  • janv→janv。

  • févr→févr。

  • avr→avr。

  • juil→juil。

  • 9月→9月。

  • oct→oct。

  • nov→nov。

  • déc→déc。

  • " janv " → " janv. "
  • " févr " → " févr. "
  • " avr " → " avr. "
  • " juil " → " juil. "
  • " sept " → " sept. "
  • " oct " → " oct. "
  • " nov " → " nov. "
  • " déc " → " déc. "

在现实世界中,我会添加一些健全性检查,以确保输入符合我们的期望,例如在中间有两个空格,在开头没有空格或结束。

In the real world, I would add some sanity-checks to ensure the input matches our expectations such as having two spaces in middle and none on the beginning or end.

Java 8及更高版本内置了java.time框架。这些新类取代了旧的java.util.Date/.Calendar和相关的类,这些类已被证明设计糟糕,令人困惑且麻烦。新的java.time类受到 Joda-Time 的启发,由 JSR 310 ,由 ThreeTen-Extra 项目,在 Oracle教程向后移植到Java 6& 7 以及向后移植到Android

Java 8 and later comes with the java.time framework built-in. These new classes supplant the old java.util.Date/.Calendar and related classes that have proven to be poorly designed, confusing, and troublesome. The new java.time classes are inspired by Joda-Time, defined by JSR 310, extended by the ThreeTen-Extra project, explained in the Oracle Tutorial, and backported to Java 6 & 7 as well as backported to Android.

java.time类包括方便的 枚举 getDisplayName 生成本地化的月份名称。

The java.time classes include the handy Month enum. The getDisplayName generates localized name of month.

同样 DateTimeFormatter 类还会生成本地化文本。调用 ofLocalized ... 方法。

System.out.println ( "US | Québec | France" );
for ( Month month : Month.values () ) {
    TextStyle style = TextStyle.SHORT;
    String us = month.getDisplayName ( style , Locale.US );
    String quebec = month.getDisplayName ( style , Locale.CANADA_FRENCH );
    String france = month.getDisplayName ( style , Locale.FRANCE );
    System.out.println ( us + " | " + quebec + " | " + france );
}

我们在java.time中获得与Joda-Time中相同的行为: 在法语中缩写的月份有一段时间。月份名称全部小写。

We get the same behavior in java.time as seen in Joda-Time: In French the abbreviated months have a period. And month names are entirely lowercase.

US | Québec | France
Jan | janv. | janv.
Feb | févr. | févr.
Mar | mars | mars
Apr | avr. | avr.
May | mai | mai
Jun | juin | juin
Jul | juil. | juil.
Aug | août | août
Sep | sept. | sept.
Oct | oct. | oct.
Nov | nov. | nov.
Dec | déc. | déc.

这篇关于用Java解析法语日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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