2017-07-31 68 views
-1

我正在研究支持多種語言(英語和西班牙語)的應用程序。我遇到了一個簡單的日期格式對話,當Locale = es時,它只會在Android 7.0和更高版本上失敗。SimpleDateFormat不適用於7.0及以上版本

這很奇怪,因爲相同的代碼在6.0和以前的SDK版本上工作得非常好。我也檢查了https://developer.android.com/guide/topics/resources/multilingual-support.html 這並沒有太大的幫助。

這是代碼

public String getDateTimeInDeviceLocale(Context context, String time) { 
    String output = "";/*Tue, 27 Aug 2013 18:10:00 Z*/ 
    SimpleDateFormat utcFormat = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss 'Z'", Locale.getDefault()); 
    utcFormat.setTimeZone(TimeZone.getTimeZone("UTC")); 

    try { 
     if (null != time) { 
      Date date = new Date(); 
      date = utcFormat.parse(time); //Throws exception only on Espanol 
      SimpleDateFormat format; 
      if (is24HourFormat(context)) { 
       format = new SimpleDateFormat("EEE MMM dd, yyyy HH:mm", Locale.getDefault()); 
      }else { 
       format = new SimpleDateFormat("EEE MMM dd, yyyy hh:mm aa", Locale.getDefault()); 
      } 
      output = format.format(date); 
      String timeZoneName = TimeZone.getDefault().getDisplayName(
        TimeZone.getDefault().inDaylightTime(new Date()), 
        TimeZone.SHORT); 
      if (!TextUtils.isEmpty(timeZoneName)) { 
       output = output + " " + timeZoneName; 
      } 
     } 
    } catch (ParseException e) { 
    // Unparseable date 
    } 

感謝您的幫助。

+0

也許'字符串time'參數已經改變 – nuvio

+0

你爲什麼不單元測試這一點,改變'getDateTimeInDeviceLocale'參數接受以下'getDateTimeInDeviceLocale(字符串時,布爾is24Hours,區域設置區域)'這將是一個容易得多測試並重現錯誤。 – nuvio

+0

如果星期幾和月份總是英文(例如'Tue'和'Aug'),則在解析 – 2017-08-04 13:34:50

回答

-1

我測試,似乎沒什麼問題,檢查應用程序

@Test 
public void testGetDatetimeInDeviceLocale_en() throws ParseException{ 
    Locale english = Locale.ENGLISH; 
    SimpleDateFormat utcFormat = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss 'Z'", english); 
    String testableDate = utcFormat.format(new Date(0)); 
    // test 24 hours format 
    assertEquals("Thu Jan 01, 1970 02:00 BST", getDateTimeInDeviceLocale(true, testableDate, english)); 
    // test AM PM format 
    assertEquals("Thu Jan 01, 1970 02:00 AM BST", getDateTimeInDeviceLocale(false, testableDate, english)); 
} 

@Test 
public void testGetDatetimeInDeviceLocale_es() throws ParseException{ 
    Locale spanish = Locale.forLanguageTag("es"); 
    SimpleDateFormat utcFormat = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss 'Z'", spanish); 
    String testableDate = utcFormat.format(new Date(0)); 
    // test 24 hours format 
    assertEquals("jue ene 01, 1970 02:00 BST", getDateTimeInDeviceLocale(true, testableDate, spanish)); 
    // test AM PM format 
    assertEquals("jue ene 01, 1970 02:00 AM BST", getDateTimeInDeviceLocale(false, testableDate, spanish)); 
} 

附註的其他領域正如我在評論上述建議你應該你的方法簽名更改爲以下:

public String getDateTimeInDeviceLocale(boolean is24Hours, String time, Locale locale) throws ParseException 

P.P.S我已刪除你釣到的魚,讓異常拋出以獲得錯誤的反饋。

+0

時必須使用'Locale.ENGLISH'而不是'getDefault()'。正如我的問題所述,它只會失敗在Android 7.0及以上版本,當選擇的語言不是en_US(在我的情況下是西班牙語) –

+0

它與Locale有關 –

相關問題