java.text.ParseException:无法解析的日期(德语,乌克兰语)

时间:2013-05-15 21:45:05

标签: java date locale date-parsing

我需要用11种不同的语言解析日期。到目前为止,只有两个给我带来麻烦:德国和乌克兰。

package com.example;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

public class TestDate {

    private static SimpleDateFormat DATE_SDF;

    public static void main(String[] args) {
        // German
        DATE_SDF = new SimpleDateFormat("dd MMM yyyy", Locale.GERMAN);
        DATE_SDF.setLenient(false);
        String[] germanDate = {"11 Jan 1970", "11 Feb 1970", "11 Mär 1970", 
                "11 Apr 1970", "11 Mai 1970", "11 Jun 1970",
                "11 Jul 1970", "11 Aug 1970", "11 Sep 1970", 
                "11 Okt 1970", "11 Nov 1970", "11 Dez 1970"};
        for (String germanDateIterator: germanDate) {
            try {
                Date parsedGermanDate = DATE_SDF.parse(germanDateIterator);
                System.out.println("parsedGermanDate:" + parsedGermanDate);
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }

        // Ukrainian
        DATE_SDF = new SimpleDateFormat("dd MMM yyyy", new Locale("uk"));
        DATE_SDF.setLenient(false);
        String[] ukDate = {"11 Січ 1970", "11 Лют 1970", "11 Бер 1970", 
                "11 Кві 1970", "11 Тра 1970", "11 Чер 1970", 
                "11 Лип 1970", "11 Сер 1970", "11 Вер 1970", 
                "11 Жов 1970", "11 Лис 1970", "11 Гру 1970"};
        for (String ukDateIterator: ukDate) {
            try {
                Date parsedUkDate = DATE_SDF.parse(ukDateIterator);
                System.out.println("parsedUkrainianDate:" + parsedUkDate);
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }
    }
}

此测试的输出为:

parsedGermanDate:Sun Jan 11 00:00:00 MST 1970
parsedGermanDate:Wed Feb 11 00:00:00 MST 1970
java.text.ParseException: Unparseable date: "11 Mär 1970"
    at java.text.DateFormat.parse(Unknown Source)
    at com.example.TestDate.main(TestDate.java:19) 
parsedGermanDate:Sat Apr 11 00:00:00 MST 1970
parsedGermanDate:Mon May 11 00:00:00 MDT 1970
parsedGermanDate:Thu Jun 11 00:00:00 MDT 1970
parsedGermanDate:Sat Jul 11 00:00:00 MDT 1970
parsedGermanDate:Tue Aug 11 00:00:00 MDT 1970
parsedGermanDate:Fri Sep 11 00:00:00 MDT 1970
parsedGermanDate:Sun Oct 11 00:00:00 MDT 1970
parsedGermanDate:Wed Nov 11 00:00:00 MST 1970
parsedGermanDate:Fri Dec 11 00:00:00 MST 1970
parsedUkrainianDate:Sun Jan 11 00:00:00 MST 1970
parsedUkrainianDate:Wed Feb 11 00:00:00 MST 1970
parsedUkrainianDate:Wed Mar 11 00:00:00 MST 1970
java.text.ParseException: Unparseable date: "11 Кві 1970"
    at java.text.DateFormat.parse(Unknown Source)
    at com.example.TestDate.main(TestDate.java:32)
java.text.ParseException: Unparseable date: "11 Тра 1970"
    at java.text.DateFormat.parse(Unknown Source)
    at com.example.TestDate.main(TestDate.java:32)
java.text.ParseException: Unparseable date: "11 Чер 1970"
    at java.text.DateFormat.parse(Unknown Source)
    at com.example.TestDate.main(TestDate.java:32)
parsedUkrainianDate:Sat Jul 11 00:00:00 MDT 1970
java.text.ParseException: Unparseable date: "11 Сер 1970"
    at java.text.DateFormat.parse(Unknown Source)
    at com.example.TestDate.main(TestDate.java:32)
parsedUkrainianDate:Fri Sep 11 00:00:00 MDT 1970
java.text.ParseException: Unparseable date: "11 Жов 1970"
    at java.text.DateFormat.parse(Unknown Source)
    at com.example.TestDate.main(TestDate.java:32)
java.text.ParseException: Unparseable date: "11 Лис 1970"
    at java.text.DateFormat.parse(Unknown Source)
    at com.example.TestDate.main(TestDate.java:32)
java.text.ParseException: Unparseable date: "11 Гру 1970"
    at java.text.DateFormat.parse(Unknown Source)
    at com.example.TestDate.main(TestDate.java:32)

为什么其中一些工作而其他工作没有?我能做些什么让他们全部工作?我使用的是Java版“1.7.0_11”。

<小时/> 我调整了assylias提供的代码以适应我的原始示例:

package com.example;

import java.io.IOException;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Locale;

public class TestDate {

    public static void main(String[] args) throws IOException {
        List<Locale> localeList = new ArrayList<Locale>();
        localeList.add(new Locale("de"));
        localeList.add(new Locale("en"));
        localeList.add(new Locale("es"));
        localeList.add(new Locale("fr"));
        localeList.add(new Locale("it"));
        localeList.add(new Locale("pt"));
        localeList.add(new Locale("ru"));
        localeList.add(new Locale("uk"));
        localeList.add(new Locale("ko"));
        localeList.add(new Locale("ja"));
        localeList.add(new Locale("zh"));
        for (Locale currentLocale : localeList) {
            DateFormat df;
            SimpleDateFormat DATE_SDF;
            if (currentLocale.toString().equals("ja")) {
                df = new SimpleDateFormat("MMMM", currentLocale);
                DATE_SDF = new SimpleDateFormat("dd MMMM yyyy", currentLocale);
            } else {
                df = new SimpleDateFormat("MMM", currentLocale);
                DATE_SDF = new SimpleDateFormat("dd MMM yyyy", currentLocale);
            }
            Calendar cal = Calendar.getInstance();
            for (int i = 0; i < 12; i++) {
                cal.set(Calendar.MONTH, i);
                String dateString = "11 " + df.format(cal.getTime()) + " 1970";
                Date parsedDate = null;
                try {
                    parsedDate = DATE_SDF.parse(dateString);
                } catch (ParseException e) {
                    e.printStackTrace();
                }
                System.out.println("parsedDate " + currentLocale + " (" + df.format(cal.getTime()) + "):" + parsedDate);
            }
        }
    }
}

结果如下:

parsedDate de (Jan):Sun Jan 11 00:00:00 MST 1970
parsedDate de (Feb):Wed Feb 11 00:00:00 MST 1970
parsedDate de (Mrz):Wed Mar 11 00:00:00 MST 1970
parsedDate de (Apr):Sat Apr 11 00:00:00 MST 1970
parsedDate de (Mai):Mon May 11 00:00:00 MDT 1970
parsedDate de (Jun):Thu Jun 11 00:00:00 MDT 1970
parsedDate de (Jul):Sat Jul 11 00:00:00 MDT 1970
parsedDate de (Aug):Tue Aug 11 00:00:00 MDT 1970
parsedDate de (Sep):Fri Sep 11 00:00:00 MDT 1970
parsedDate de (Okt):Sun Oct 11 00:00:00 MDT 1970
parsedDate de (Nov):Wed Nov 11 00:00:00 MST 1970
parsedDate de (Dez):Fri Dec 11 00:00:00 MST 1970
parsedDate en (Jan):Sun Jan 11 00:00:00 MST 1970
parsedDate en (Feb):Wed Feb 11 00:00:00 MST 1970
parsedDate en (Mar):Wed Mar 11 00:00:00 MST 1970
parsedDate en (Apr):Sat Apr 11 00:00:00 MST 1970
parsedDate en (May):Mon May 11 00:00:00 MDT 1970
parsedDate en (Jun):Thu Jun 11 00:00:00 MDT 1970
parsedDate en (Jul):Sat Jul 11 00:00:00 MDT 1970
parsedDate en (Aug):Tue Aug 11 00:00:00 MDT 1970
parsedDate en (Sep):Fri Sep 11 00:00:00 MDT 1970
parsedDate en (Oct):Sun Oct 11 00:00:00 MDT 1970
parsedDate en (Nov):Wed Nov 11 00:00:00 MST 1970
parsedDate en (Dec):Fri Dec 11 00:00:00 MST 1970
parsedDate es (ene):Sun Jan 11 00:00:00 MST 1970
parsedDate es (feb):Wed Feb 11 00:00:00 MST 1970
parsedDate es (mar):Wed Mar 11 00:00:00 MST 1970
parsedDate es (abr):Sat Apr 11 00:00:00 MST 1970
parsedDate es (may):Mon May 11 00:00:00 MDT 1970
parsedDate es (jun):Thu Jun 11 00:00:00 MDT 1970
parsedDate es (jul):Sat Jul 11 00:00:00 MDT 1970
parsedDate es (ago):Tue Aug 11 00:00:00 MDT 1970
parsedDate es (sep):Fri Sep 11 00:00:00 MDT 1970
parsedDate es (oct):Sun Oct 11 00:00:00 MDT 1970
parsedDate es (nov):Wed Nov 11 00:00:00 MST 1970
parsedDate es (dic):Fri Dec 11 00:00:00 MST 1970
parsedDate fr (janv.):Sun Jan 11 00:00:00 MST 1970
parsedDate fr (févr.):Wed Feb 11 00:00:00 MST 1970
parsedDate fr (mars):Wed Mar 11 00:00:00 MST 1970
parsedDate fr (avr.):Sat Apr 11 00:00:00 MST 1970
parsedDate fr (mai):Mon May 11 00:00:00 MDT 1970
parsedDate fr (juin):Thu Jun 11 00:00:00 MDT 1970
parsedDate fr (juil.):Sat Jul 11 00:00:00 MDT 1970
parsedDate fr (août):Tue Aug 11 00:00:00 MDT 1970
parsedDate fr (sept.):Fri Sep 11 00:00:00 MDT 1970
parsedDate fr (oct.):Sun Oct 11 00:00:00 MDT 1970
parsedDate fr (nov.):Wed Nov 11 00:00:00 MST 1970
parsedDate fr (déc.):Fri Dec 11 00:00:00 MST 1970
parsedDate it (gen):Sun Jan 11 00:00:00 MST 1970
parsedDate it (feb):Wed Feb 11 00:00:00 MST 1970
parsedDate it (mar):Wed Mar 11 00:00:00 MST 1970
parsedDate it (apr):Sat Apr 11 00:00:00 MST 1970
parsedDate it (mag):Mon May 11 00:00:00 MDT 1970
parsedDate it (giu):Thu Jun 11 00:00:00 MDT 1970
parsedDate it (lug):Sat Jul 11 00:00:00 MDT 1970
parsedDate it (ago):Tue Aug 11 00:00:00 MDT 1970
parsedDate it (set):Fri Sep 11 00:00:00 MDT 1970
parsedDate it (ott):Sun Oct 11 00:00:00 MDT 1970
parsedDate it (nov):Wed Nov 11 00:00:00 MST 1970
parsedDate it (dic):Fri Dec 11 00:00:00 MST 1970
parsedDate pt (Jan):Sun Jan 11 00:00:00 MST 1970
parsedDate pt (Fev):Wed Feb 11 00:00:00 MST 1970
parsedDate pt (Mar):Wed Mar 11 00:00:00 MST 1970
parsedDate pt (Abr):Sat Apr 11 00:00:00 MST 1970
parsedDate pt (Mai):Mon May 11 00:00:00 MDT 1970
parsedDate pt (Jun):Thu Jun 11 00:00:00 MDT 1970
parsedDate pt (Jul):Sat Jul 11 00:00:00 MDT 1970
parsedDate pt (Ago):Tue Aug 11 00:00:00 MDT 1970
parsedDate pt (Set):Fri Sep 11 00:00:00 MDT 1970
parsedDate pt (Out):Sun Oct 11 00:00:00 MDT 1970
parsedDate pt (Nov):Wed Nov 11 00:00:00 MST 1970
parsedDate pt (Dez):Fri Dec 11 00:00:00 MST 1970
parsedDate ru (янв):Sun Jan 11 00:00:00 MST 1970
parsedDate ru (фев):Wed Feb 11 00:00:00 MST 1970
parsedDate ru (мар):Wed Mar 11 00:00:00 MST 1970
parsedDate ru (апр):Sat Apr 11 00:00:00 MST 1970
parsedDate ru (май):Mon May 11 00:00:00 MDT 1970
parsedDate ru (июн):Thu Jun 11 00:00:00 MDT 1970
parsedDate ru (июл):Sat Jul 11 00:00:00 MDT 1970
parsedDate ru (авг):Tue Aug 11 00:00:00 MDT 1970
parsedDate ru (сен):Fri Sep 11 00:00:00 MDT 1970
parsedDate ru (окт):Sun Oct 11 00:00:00 MDT 1970
parsedDate ru (ноя):Wed Nov 11 00:00:00 MST 1970
parsedDate ru (дек):Fri Dec 11 00:00:00 MST 1970
parsedDate uk (січ):Sun Jan 11 00:00:00 MST 1970
parsedDate uk (лют):Wed Feb 11 00:00:00 MST 1970
parsedDate uk (бер):Wed Mar 11 00:00:00 MST 1970
parsedDate uk (квіт):Sat Apr 11 00:00:00 MST 1970
parsedDate uk (трав):Mon May 11 00:00:00 MDT 1970
parsedDate uk (черв):Thu Jun 11 00:00:00 MDT 1970
parsedDate uk (лип):Sat Jul 11 00:00:00 MDT 1970
parsedDate uk (серп):Tue Aug 11 00:00:00 MDT 1970
parsedDate uk (вер):Fri Sep 11 00:00:00 MDT 1970
parsedDate uk (жовт):Sun Oct 11 00:00:00 MDT 1970
parsedDate uk (лист):Wed Nov 11 00:00:00 MST 1970
parsedDate uk (груд):Fri Dec 11 00:00:00 MST 1970
parsedDate ko (1월):Sun Jan 11 00:00:00 MST 1970
parsedDate ko (2월):Wed Feb 11 00:00:00 MST 1970
parsedDate ko (3월):Wed Mar 11 00:00:00 MST 1970
parsedDate ko (4월):Sat Apr 11 00:00:00 MST 1970
parsedDate ko (5월):Mon May 11 00:00:00 MDT 1970
parsedDate ko (6월):Thu Jun 11 00:00:00 MDT 1970
parsedDate ko (7월):Sat Jul 11 00:00:00 MDT 1970
parsedDate ko (8월):Tue Aug 11 00:00:00 MDT 1970
parsedDate ko (9월):Fri Sep 11 00:00:00 MDT 1970
parsedDate ko (10월):Sun Oct 11 00:00:00 MDT 1970
parsedDate ko (11월):Wed Nov 11 00:00:00 MST 1970
parsedDate ko (12월):Fri Dec 11 00:00:00 MST 1970
parsedDate ja (1月):Sun Jan 11 00:00:00 MST 1970
parsedDate ja (2月):Wed Feb 11 00:00:00 MST 1970
parsedDate ja (3月):Wed Mar 11 00:00:00 MST 1970
parsedDate ja (4月):Sat Apr 11 00:00:00 MST 1970
parsedDate ja (5月):Mon May 11 00:00:00 MDT 1970
parsedDate ja (6月):Thu Jun 11 00:00:00 MDT 1970
parsedDate ja (7月):Sat Jul 11 00:00:00 MDT 1970
parsedDate ja (8月):Tue Aug 11 00:00:00 MDT 1970
parsedDate ja (9月):Fri Sep 11 00:00:00 MDT 1970
parsedDate ja (10月):Sun Oct 11 00:00:00 MDT 1970
parsedDate ja (11月):Wed Nov 11 00:00:00 MST 1970
parsedDate ja (12月):Fri Dec 11 00:00:00 MST 1970
parsedDate zh (一月):Sun Jan 11 00:00:00 MST 1970
parsedDate zh (二月):Wed Feb 11 00:00:00 MST 1970
parsedDate zh (三月):Wed Mar 11 00:00:00 MST 1970
parsedDate zh (四月):Sat Apr 11 00:00:00 MST 1970
parsedDate zh (五月):Mon May 11 00:00:00 MDT 1970
parsedDate zh (六月):Thu Jun 11 00:00:00 MDT 1970
parsedDate zh (七月):Sat Jul 11 00:00:00 MDT 1970
parsedDate zh (八月):Tue Aug 11 00:00:00 MDT 1970
parsedDate zh (九月):Fri Sep 11 00:00:00 MDT 1970
parsedDate zh (十月):Sun Oct 11 00:00:00 MDT 1970
parsedDate zh (十一月):Wed Nov 11 00:00:00 MST 1970
parsedDate zh (十二月):Fri Dec 11 00:00:00 MST 1970

一切顺利。现在我只需要调整我的JQuery日期格式和翻译以匹配。我必须将for循环中的迭代长度从11更新到12以包括12月。 谢谢assylias !!

1 个答案:

答案 0 :(得分:7)

这些不是有效日期 - 请尝试使用此代码获取正确的短月份名称。因此,您会注意到4月份是Квіт,而不是Кві。至于德国,March应该是Mrz,而不是März

public static void main(String[] args) throws IOException {
    Locale ukLocale = new Locale("uk");
    DateFormat df = new SimpleDateFormat("MMM", ukLocale);
    Calendar cal = Calendar.getInstance();
    for (int i = 0; i < 11; i++) {
        cal.set(Calendar.MONTH, i);
        System.out.println(df.format(cal.getTime()));
    }
}

输出:

січ
лют
бер
квіт
трав
черв
лип
серп
вер
жовт
лист