将从服务器收到的时间字符串转换为本地时间

时间:2013-06-21 17:38:56

标签: android datetime mysqli

用户将消息发布到我的服务器,这些消息默认存储在数据库中,其中一列是CURRENT_TIMESTAMP。如下所示:

Message Table:

Sender |  Content |  SentTime 

Tom    | hi there | 2013-06-19 00:13:21

当用户将消息发布到我的服务器时,他/她不需要发布他/她的当地时间。我的数据库记录该值自动。我觉得这比用户将他/她的时间与消息一起发布更好。

稍后用户将从服务器检索消息以及发送时间。我想知道如何将该时间转换为用户的当地时间。

我正在使用Json在服务器上进行编码,然后在我的应用中解码数据。所以从服务器获取的时间将是一个字符串,如:2013-06-19 00:13:21我想我需要解析这个字符串以开始?

1 个答案:

答案 0 :(得分:2)

你必须使用SimpeDateFormat解析服务器返回的String:

SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
simpleDateFormat.setTimeZone(TimeZone.getTimeZone("EST"));
Date date = simpleDateFormat.parse("2013-06-19 00:13:21");

TimeZone destTz = TimeZone.getTimeZone("yourtimezone");
simpleDateFormat.setTimeZone(destTz);
String result = simpleDateFormat.format(date);