2016-04-26 578 views
2

我有這個日期字符串:2016-04-26T09:14:10.477Z這是UTC時區。我想將其轉換爲用戶本地時區,所以如果是格林威治標準時間+02:00,我需要一個值爲2016-04-26T11:14:10.477Z的日期(請注意9個更改爲11)。如何將UTC日期轉換爲地區GMT時間android

我一直在使用這個至今:

val sdf = SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS", Locale.getDefault()) 
val now = Date() 

val limit = sdf.parse(coupon.usedAt) 

這裏limit = Tue Apr 26 09:14:10 GMT+02:00 2016這是不正確的,我想。

那麼我怎樣才能正確地轉換它?謝謝!

編輯:我的問題不同於this one,因爲我需要使用對象SimpleDateFormat而不是Date一個

+0

您可以通過時間戳的1000轉換UTC devide於北京時間 – Naitik

+0

可能的複製[如何我使用Java或GMT獲取當前日期和時間?](http://stackoverflow.com/questions/308683/how-can-i-get-the-current-date-and-time-in-utc-或-GMT-在-JA va) –

+0

使用此代碼嘗試。 [點擊這裏查看日期和時間的所有格式](https://stackoverflow.com/questions/5369682/get-current-time-and-date-on-android/34328449#34328449) – Amitsharma

回答

3

試試這個

SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 
simpleDateFormat.setTimeZone(TimeZone.getTimeZone("UTC")); 
Date myDate = simpleDateFormat.parse(rawQuestion.getString("AskDateTime")); 
0

試試這個。

String dateString = "04/26/2016 10:22:00 AM"; 
    SimpleDateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy hh:mm:ss aa"); 
    Date convertedDate = new Date(); 
    try { 
     convertedDate = dateFormat.parse(dateString); 
    } catch (ParseException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 

    Calendar calendar = Calendar.getInstance(); 
    calendar.setTimeZone(TimeZone.getTimeZone("UTC")); 
    calendar.setTime(convertedDate); 
    Date time = calendar.getTime(); 
    SimpleDateFormat outputFmt = new SimpleDateFormat("MMM dd, yyy h:mm a zz"); 
    String utcTime = outputFmt.format(time); 
0
DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSXXX"); 
    Date date = sdf.parse("2016-04-26T09:14:10.477Z"); 
    sdf.setTimeZone(TimeZone.getTimeZone("IST")); 
    System.out.println(sdf.format(date)); 
0

試試這個,替換格式

public static final String DATE_FORMAT  = "yyyy-MM-dd'T'HH:mm:ss'Z'"; 

public static String getFormattedLocalTimeFromUtc (String utcTimeStamp, String outputFormat) { 

    String formattedTime = null; 
    if (!TextUtils.isEmpty (utcTimeStamp)) { 
    if (utcTimeStamp.contains ("T")) { 

     String localTime = null; 
     SimpleDateFormat sdf = new SimpleDateFormat (DATE_FORMAT, Locale.getDefault()); 
     sdf.setTimeZone (TimeZone.getTimeZone ("UTC")); 
     try { 
      localTime = sdf.parse (utcTimeStamp).toString(); 
     } 
     catch (ParseException e) { 
      e.printStackTrace(); 
     } 

     DateFormat inputDateFormat = new SimpleDateFormat ("EEE MMM dd HH:mm:ss z yyyy"); 
     inputDateFormat.setTimeZone (TimeZone.getTimeZone ("UTC")); 
     Date date = null; 
     try { 
      date = inputDateFormat.parse (localTime); 
     } 
     catch (ParseException e) { 
      e.printStackTrace(); 
     } 

     DateFormat outputDateFormat = new SimpleDateFormat (outputFormat); 
     formattedTime = outputDateFormat.format (date); 
    } 
    } 
    return formattedTime; 
} 
相關問題