2016-05-06 24 views
5
DateFormat formatter = new SimpleDateFormat("hh:mmaa"); 
formatter.parse("01:20pm") 

sto cercando di analizzare i tempi nel formato 13:20. Se faccio funzionare il codice di cui sopra, ottengo la seguente eccezione:Parsing semplici volte in formato HH: MMAA

java.text.ParseException: Unparseable date: "01:20pm" 
    at java.text.DateFormat.parse(DateFormat.java:366) 

Per quanto riguarda il formato ho messo nel costruttore SimpleDateFormat, non vedo nulla di male. Cosa è andato storto qui?

+2

Può essere che sia questione locale? Qual è il tuo predefinito? Prova a passare Locale.US –

+0

quale versione di JDK stai usando, questo funziona abbastanza bene con JDK_1.8 –

+0

Signore, era un problema locale !!!! Che cosa ... Perché mai il locale dovrebbe essere un problema qui? Ci sono altre localizzazioni che interpretano hh: mmaa ampiamente differenti? – Xiagua

risposta

7

Le impostazioni internazionali del sistema non devono riconoscere AM/PM. Utilizzare uno Locale che fa. Qualcosa di simile,

DateFormat formatter = new SimpleDateFormat("hh:mmaa", Locale.US); 

Oppure, in Java 8 +, utilizzare la nuova API java.time come

LocalTime lt = LocalTime.parse("01:20pm", 
     DateTimeFormatter.ofPattern("hh:mmaa", Locale.US)); 
+1

Era un problema locale. 10 PUNTI PER JAVA – Xiagua

1

Numero e data l'analisi in Java utilizza le impostazioni internazionali per specificare, bene, i simboli specifici della locale. In questo caso, è principalmente il valore pm che viene rifiutato.

Per confermare questo, ecco un pezzo di codice per esercitare tutte le impostazioni locali disponibili nella VM.

Per le località che non funzionano, ero curioso di vedere perché, così invece di analizzare un tempo, formattare invece un tempo valido. Doveva abilitare l'output UTF-8, ma è interessante vedere.

La parte veramente interessante è che tutti (es) locali spagnoli, tranne la variante Stati Uniti (es_US) funziona bene. Hmmm ........

Set<String> good = new TreeSet<>(); 
Set<String> bad = new TreeSet<>(); 
for (Locale locale : Locale.getAvailableLocales()) { 
    try { 
     new SimpleDateFormat("hh:mmaa", locale).parse("01:20pm"); 
     good.add(locale.toLanguageTag()); 
    } catch (ParseException e) { 
     bad.add(locale.toLanguageTag()); 
    } 
} 
System.out.println("Good locales: " + good); 

Date time = new SimpleDateFormat("hh:mmaa", Locale.ENGLISH).parse("01:20pm"); 
System.out.println("Bad locales:"); 
for (String languageTag : bad) 
    System.out.printf(" %-5s: %s%n", languageTag, new SimpleDateFormat("hh:mmaa", Locale.forLanguageTag(languageTag)).format(time)); 

USCITA

Good locales: [be, be-BY, bg, bg-BG, ca, ca-ES, da, da-DK, de, de-AT, de-CH, de-DE, de-GR, de-LU, en, en-AU, en-CA, en-GB, en-IE, en-IN, en-MT, en-NZ, en-PH, en-SG, en-US, en-ZA, es, es-AR, es-BO, es-CL, es-CO, es-CR, es-CU, es-DO, es-EC, es-ES, es-GT, es-HN, es-MX, es-NI, es-PA, es-PE, es-PR, es-PY, es-SV, es-UY, es-VE, et, et-EE, fr, fr-BE, fr-CA, fr-CH, fr-FR, fr-LU, he, he-IL, hi, hr, hr-HR, id, id-ID, is, is-IS, it, it-CH, it-IT, lt, lt-LT, lv, lv-LV, mk, mk-MK, ms, ms-MY, nl, nl-BE, nl-NL, nn-NO, no, no-NO, pl, pl-PL, pt, pt-BR, pt-PT, ro, ro-RO, ru, ru-RU, sk, sk-SK, sl, sl-SI, sr, sr-BA, sr-CS, sr-Latn, sr-Latn-BA, sr-Latn-ME, sr-Latn-RS, sr-ME, sr-RS, tr, tr-TR, uk, uk-UA, und] 
Bad locales: 
    ar : 01:20م 
    ar-AE: 01:20م 
    ar-BH: 01:20م 
    ar-DZ: 01:20م 
    ar-EG: 01:20م 
    ar-IQ: 01:20م 
    ar-JO: 01:20م 
    ar-KW: 01:20م 
    ar-LB: 01:20م 
    ar-LY: 01:20م 
    ar-MA: 01:20م 
    ar-OM: 01:20م 
    ar-QA: 01:20م 
    ar-SA: 01:20م 
    ar-SD: 01:20م 
    ar-SY: 01:20م 
    ar-TN: 01:20م 
    ar-YE: 01:20م 
    cs : 01:20odp. 
    cs-CZ: 01:20odp. 
    el : 01:20μμ 
    el-CY: 01:20ΜΜ 
    el-GR: 01:20μμ 
    es-US: 01:20p.m. 
    fi : 01:20ip. 
    fi-FI: 01:20ip. 
    ga : 01:20p.m. 
    ga-IE: 01:20p.m. 
    hi-IN: ०१:२०अपराह्न 
    hu : 01:20DU 
    hu-HU: 01:20DU 
    ja : 01:20午後 
    ja-JP: 01:20午後 
    ja-JP-u-ca-japanese-x-lvariant-JP: 01:20午後 
    ko : 01:20오후 
    ko-KR: 01:20오후 
    mt : 01:20WN 
    mt-MT: 01:20WN 
    sq : 01:20MD 
    sq-AL: 01:20MD 
    sv : 01:20em 
    sv-SE: 01:20em 
    th : 01:20หลังเที่ยง 
    th-TH: 01:20หลังเที่ยง 
    th-TH-u-nu-thai-x-lvariant-TH: ๐๑:๒๐หลังเที่ยง 
    vi : 01:20CH 
    vi-VN: 01:20CH 
    zh : 01:20下午 
    zh-CN: 01:20下午 
    zh-HK: 01:20下午 
    zh-SG: 01:20下午 
    zh-TW: 01:20下午 
Problemi correlati