2013-05-15 3 views
7

날짜를 11 가지 언어로 파싱해야합니다. 지금까지 단 두 명이 나를 괴롭혔습니다 : 독일과 우크라이나.java.text.ParseException : Unparseable date (독일어, 우크라이나어)

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"을 사용하고 있습니다.

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 

모든 밖으로 일 :

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); 
      } 
     } 
    } 
} 

이 결과 :


나는 assylias 내 원래의 예에 맞게 제공되는 코드를 조정했다. 이제는 JQuery 날짜 형식과 번역을 일치시켜야합니다. 12 월을 포함하도록 for 루프의 반복 길이를 11에서 12로 업데이트해야했습니다. 감사합니다.

+0

부호화 문제 일종의. 러시아 문자가 무엇이 문제인지를 모릅니다. – Coffee

+0

DATE_SDF와 같은 날짜를 사용하여 날짜 형식을 지정하고 생성 된 내용을 확인하십시오. 그렇게하면 파싱 할 때 예상되는 것을 볼 수 있습니다. –

+0

날짜를 다른 언어로 파싱하지 않고, 날짜가 다른 언어로 된 Date를 포함하는 String을 파싱합니다. 오류는 자기 설명 적으로 보입니다. 날짜를 구문 분석 할 수 없습니다. 아마도 잘못된 월 이름으로 해석 할 수 있습니다. –

답변

7

다음은 유효한 날짜가 아닙니다. 적절한 짧은 월 이름을 얻으려면이 코드를 사용해보십시오. 예를 들어 4 월이 Квіт이 아니라, Кві이 아니라는 것을 알 수 있습니다. 독일의 경우 3 월은 이 아니라 Mrz이어야합니다. 출력

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())); 
    } 
} 

:

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