2013-10-27 39 views
0

我想從日期從JSON格式轉換爲我的本地時間。通過使用SimpleDateFormat沒有得到適當的時間

我得到的時間爲10/27/2013 5:58:02 PM,我需要轉換爲我的當地時間,這是+5:30

但是,我得到10/27/2013 6:28:02

我的代碼是

SimpleDateFormat simpleDateFormat = new SimpleDateFormat("M/d/yyyy H:mm:ss a"); 
    SimpleDateFormat longDateFormat=new SimpleDateFormat("M/d/yyyy H:mm:ss"); 
    simpleDateFormat.setTimeZone(TimeZone.getTimeZone("UTC")); 

     String formattedDate=""; 
     try 
     { 
      Date myDate = simpleDateFormat.parse(mDateAndTime); 

      formattedDate = longDateFormat.format(myDate); 
     } catch (ParseException e) 
     { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
+0

如果刪除線... simpleDateFormat.setTimeZone(TimeZone.getTimeZone( 「UTC」)); 可能是有效的。我不確定,因爲我無法測試它。請嘗試 –

+0

thnx的回覆,但我越來越10/27/2013 0:58:02 –

回答

1

我認爲這將有助於您:

SimpleDateFormat simpleDateFormat = new SimpleDateFormat("M/d/yyyy hh:mm:ss a"); 
SimpleDateFormat longDateFormat = new SimpleDateFormat("M/d/yyyy HH:mm:ss z"); 
simpleDateFormat.setTimeZone(TimeZone.getTimeZone("UTC")); 
try 
{ 
    Date myDate = simpleDateFormat.parse("10/25/2013 02:00:00 PM"); 
    String now = simpleDateFormat.format(myDate.getTime()); 
    System.out.println("12 hour format : " + now); 
    String time24 = longDateFormat.format(simpleDateFormat.parse(now)); 
    System.out.println("24 hour format : " + time24); 
} catch (ParseException e) 
{ 
    // TODO Auto-generated catch block 
    e.printStackTrace(); 
} 
+0

不,我得到10/27/2013 0:58:02 AM +0530。這裏是什麼「Z」? –

+0

你想要哪種時間格式? –

+0

由longDateFormat指定的24小時格式。 –

相關問題