2017-02-17 39 views
0

空,我有:的Java的getTime()返回格式的字符串

String stringDate = "2017-02-16T15:00:00Z" 

我打算將它轉換成一個日期和之後,我想轉換爲龍。這裏是我的代碼:

private void normalizeDate(ContentValues values) { 
    // normalize the date value 
    if (values.containsKey(SmogContract.MeasurementEntry.COLUMN_FROM_DATE_TIME)) { 
     Date date = convertDateFromStringToDate(values.getAsString(SmogContract.MeasurementEntry.COLUMN_FROM_DATE_TIME)); 
     long fromDateValue = date.getTime(); 
     values.put(SmogContract.MeasurementEntry.COLUMN_FROM_DATE_TIME, SmogContract.normalizeDate(fromDateValue)); 
    } 
    if (values.containsKey(SmogContract.MeasurementEntry.COLUMN_TILL_DATE_TIME)) { 
     long fromDateValue = values.getAsLong(SmogContract.MeasurementEntry.COLUMN_TILL_DATE_TIME); 
     values.put(SmogContract.MeasurementEntry.COLUMN_TILL_DATE_TIME, SmogContract.normalizeDate(fromDateValue)); 
    } 
} 

private Date convertDateFromStringToDate(String stringDate){ 
    DateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'"); 
    format.setTimeZone(TimeZone.getTimeZone("GMT")); 
    Date convertedFromStringDate = null; 
    try { 
     convertedFromStringDate = format.parse(stringDate); 
    } catch (ParseException e) { 
     e.printStackTrace(); 
    } 

    return convertedFromStringDate; 
} 

這裏是我得到異常:

java.lang.NullPointerException: Attempt to invoke virtual method 'long java.lang.Long.longValue()' on a null object reference 
       at com.example.marcin.smog_mapa.data.SmogProvider.normalizeDate(SmogProvider.java:109) 
       at com.example.marcin.smog_mapa.data.SmogProvider.insert(SmogProvider.java:85) 
       at android.content.ContentProvider$Transport.insert(ContentProvider.java:263) 
       at android.content.ContentProviderNative.onTransact(ContentProviderNative.java:163) 
       at android.os.Binder.execTransact(Binder.java:453) 
+0

請看看控制檯。驚喜! –

+1

http://stackoverflow.com/questions/19112357/java-simpledateformatyyyy-mm-ddthhmmssz-gives-timezone-as-ist – Laazo

+1

[Java SimpleDateFormat(「yyyy-MM-dd'T'HH:mm:ss 'Z'「)給時區作爲IST](http://stackoverflow.com/questions/19112357/java-simpledateformatyyyy-mm-ddthhmmssz-gives-timezone-as-ist) – RamPrakash

回答

0

format.parse(...)語句失敗了ParseException,在null留下convertedFromStringDate。 檢查您的控制檯的堆棧跟蹤,並確保stringDate具有正確的格式。

+0

這是一個我從API響應中獲得的字符串 – wegtis

0

好的,所以這只是方法中的一個愚蠢的錯誤。

這裏是做正確:從

long fromDateValue = values.getAsLong(SmogContract.MeasurementEntry.COLUMN_TILL_DATE_TIME); 

private void normalizeDate(ContentValues values) { 
    // normalize the date value 
    if (values.containsKey(SmogContract.MeasurementEntry.COLUMN_FROM_DATE_TIME)) { 
     Date date = convertDateFromStringToDate(values.getAsString(SmogContract.MeasurementEntry.COLUMN_FROM_DATE_TIME)); 
     Log.d("ConvertedDate: ", String.valueOf(date.getTime())); 
     long fromDateValue = date.getTime(); 
     values.put(SmogContract.MeasurementEntry.COLUMN_FROM_DATE_TIME, SmogContract.normalizeDate(fromDateValue)); 
    } 
    if (values.containsKey(SmogContract.MeasurementEntry.COLUMN_TILL_DATE_TIME)) { 
     Date date = convertDateFromStringToDate(values.getAsString(SmogContract.MeasurementEntry.COLUMN_TILL_DATE_TIME)); 
     Log.d("ConvertedDate: ", String.valueOf(date.getTime())); 
     long fromDateValue = date.getTime(); 
     values.put(SmogContract.MeasurementEntry.COLUMN_TILL_DATE_TIME, SmogContract.normalizeDate(fromDateValue)); 
    } 
} 
0

的問題在哪裏valuesContentValues實例。

ContentValues.getAsLong回報您在long存儲,所以它叫Long.longValue()導致這種NullPointerException

這是一個null與此自動裝箱/拆箱,如果您有

Long wrap_long = null; 
long l = wrap_long; 

這種風險將編譯,但會在運行時拋出一個NPE,其中

long l = null; 

將永遠不會編譯,出於同樣的原因,原始值不能是null

當您解除它的值時,檢查Long值爲null是很好的。