Android 将从服务器接收的时间(即UTC时间)转换为本地时间

Android 将从服务器接收的时间(即UTC时间)转换为本地时间,android,date,time,Android,Date,Time,我从服务器接收的时间遵循UTC时区,并遵循12小时格式(2013年7月30日上午6:44:22) 任何人都可以。告诉我如何将此特定时间转换为本地时间并显示为(12:20)(必须为24小时格式) 以下是我尝试的代码: object._chatCreatedDateTime=obj.getString("CreatedDateTime"); SimpleDateFormat format = new SimpleDateFormat("HH:mm"); format.setTimeZone(Time

我从服务器接收的时间遵循UTC时区,并遵循12小时格式(2013年7月30日上午6:44:22)

任何人都可以。告诉我如何将此特定时间转换为本地时间并显示为(12:20)(必须为24小时格式)

以下是我尝试的代码:

object._chatCreatedDateTime=obj.getString("CreatedDateTime");
SimpleDateFormat format = new SimpleDateFormat("HH:mm");
format.setTimeZone(TimeZone.getTimeZone("UTC"));
date  = format.format(Date.parse(object._chatCreatedDateTime));
object._chatCreatedDateTime=date;

这里(object.\u chatCreatedDateTime)是从服务器接收到的时间,我将其转换为特定格式,并将其推回到本地数据库,但不会产生正确的结果。

以获取24小时模式使用的时间

“H:mm”

作为时间模式字符串

H表示一天中的小时(0-23)

因此,修改您的代码如下:

object._chatCreatedDateTime=obj.getString("CreatedDateTime");

SimpleDateFormat format = new SimpleDateFormat("H:mm");

format.setTimeZone(TimeZone.getTimeZone("UTC"));date = format.format(Date.parse(object._chatCreatedDateTime));

object._chatCreatedDateTime=date;

以24小时模式使用时间

“H:mm”

作为时间模式字符串

H表示一天中的小时(0-23)

因此,修改您的代码如下:

object._chatCreatedDateTime=obj.getString("CreatedDateTime");

SimpleDateFormat format = new SimpleDateFormat("H:mm");

format.setTimeZone(TimeZone.getTimeZone("UTC"));date = format.format(Date.parse(object._chatCreatedDateTime));

object._chatCreatedDateTime=date;

我希望根据我的位置调整时间使用
formatDateTime(上下文、长毫秒、整数标志)
,它根据本地约定返回日期或时间。它有一个名为flag
flag
的参数,当提供常数
FORMAT\u 24HOUR
时,该参数将以24小时格式返回时间。我希望根据我的位置调整时间使用
formatDateTime(上下文上下文、长毫秒、int标志)
根据本地约定返回日期或时间。它有一个名为flag
flag
的参数,当提供常数
FORMAT\u 24HOUR
时,该参数以24小时格式返回时间。