2016-01-06 214 views
0

我从服务器获取UTC时间,例如 - “2016-01-04T06:27:23.92”。我想将它转换为Jan 4,2016,并在本地Formate中使用。我使用下面的代码,但它不工作 -将UTC时间转换为本地android

Date localTime = new Date(commentDate); 
String format = "yyyy/MM/dd HH:mm:ss"; 
SimpleDateFormat sdf = new SimpleDateFormat(format); 
Date gmtTime = new Date(sdf.format(localTime)); 
Date fromGmt = new Date(gmtTime.getTime() + TimeZone.getDefault().getOffset(localTime.getTime())); 
commentDate = commentDate.substring(0, commentDate.lastIndexOf(".")); 
+0

你正在得到哪个错误? – CAFEBABE

+0

我在“Date localTime = new Date(commentDate);”线。我有时间在字符串合成例如 - “2016-01-04T06:27:23.92” –

+2

http://stackoverflow.com/questions/6049207/convert-utc-to-current-locale-time看到的链接 –

回答

0

试试这个

try { 
    //commentDate = "2016-01-04T06:27:23.92" 
    Date date1 = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss").parse(commentDate); 

    Date fromGmt = new Date(date1.getTime() + TimeZone.getDefault().getOffset(date1.getTime())); 
    Log.e("Date",new SimpleDateFormat("yyyy/MM/dd HH:mm:ss").format(fromGmt)); // print 2016/01/04 11:57:23 (0530 hour deviation) 
} catch (ParseException e) { 
    e.printStackTrace(); 
} 

您可以根据您的要求更改日期格式。

相关问题