2013-10-27 36 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小时格式。 –

相关问题