Muszę przeanalizować datę w 11 różnych językach. Jak dotąd tylko dwa sprawiają mi kłopoty: niemiecki i ukraiński.java.text.ParseException: Nie można odczytać daty (niemiecki, ukraiński)
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();
}
}
}
}
Wyjście dla tego testu jest:
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)
Dlaczego jest to, że niektóre z tych prac, a inni nie? Co mogę zrobić, aby wszyscy mogli pracować? Używam wersji Java "1.7.0_11".
I dostosowane kod assylias warunkiem, aby pasowały do mojego oryginalnego przykładzie:
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);
}
}
}
}
Jest to wynikiem:
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
Wszystko działało. Teraz muszę tylko dopasować format daty i tłumaczenia JQuery. Musiałem zaktualizować długość iteracji w pętli for od 11 do 12, aby uwzględnić grudzień. Dziękuję assylias !!
Jakiś kwestii kodowania. Nie wiem co, ale rosyjskie postacie powodują problemy – Coffee
Użyj tej samej daty DATE_SDF, aby sformatować daty i zobaczyć, co generuje. W ten sposób zobaczysz, czego oczekuje podczas parsowania. –
Nie analizujesz daty w różnych językach, parsujesz ciąg zawierający datę w różnych językach w postaci daty. Błąd wygląda samoopisowo: nie można przeanalizować daty, prawdopodobnie przez złą nazwę miesiąca. –