簡體   English   中英

SimpleDateFormat在某些Android版本上不起作用

[英]SimpleDateFormat does not work on some android versions

我必須閱讀以下格式的日期, 星期六,2013年1月19日00:00:00 +0100
我的格式化程序是

SimpleDateFormat format = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss Z");

當我想設置數據時:

try {
    myObject.setEndDate(format.parse(currentValue));
} catch (ParseException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}

如果我在簡單的Java項目中或在NexusHtc上使用它,則此方法非常完美,但是當我將調試器附加到Samsung s3時,它會給出解析器異常。

我希望這適用於所有版本的android和手機。

SimpleDateFormat format = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss.SSSZ");

嘗試這個:

在捕獲中打印currentValueLocale.getDefaultLocale() 語言環境最有可能阻止解析currentValue。

至少您可以在具有完全相同參數的PC上嘗試使用該代碼。

我通過在try catch中沒有簡單日期格式的日期計算日期來解決此問題。在某些手機上運行時,它不會被捕獲,而格式運行良好;在其他手機上運行時,它會被捕獲,但是日期計算得當。

   if(localName.equals("startDate") && currentValue!=null){

    try {
        myObject.setStartDate(format.parse(currentValue));
    } catch (ParseException e) {
        String  dates [] = currentValue.split(" ");
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.DAY_OF_MONTH, Integer.parseInt(dates[1]));
        cal.set(Calendar.YEAR, Integer.parseInt(dates[3]));
        cal.set(Calendar.MONTH, StaticDataUtils.months.get(dates[2]));
        String hours [] = dates[4].split(":");
        cal.set(Calendar.HOUR_OF_DAY, Integer.parseInt(hours[0]));
        cal.set(Calendar.MINUTE, Integer.parseInt(hours[1]));
        myObject.setStartDate(cal.getTime());
        e.printStackTrace();
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM