根据设备的时区显示日期 [英] Display date according to the time zone of device

查看:113
本文介绍了根据设备的时区显示日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

2010-06-14 02:21:49-0400
或2010-06-14 02:21:49 + 0400

2010-06-14 02:21:49-0400 or 2010-06-14 02:21:49+0400

以上是来自我的webservice的字符串...
有一种方法可以将此字符串转换为根据本地计算机时区的日期

The above is the string coming from my webservice... is there a way to convert this string to the date according to the local machine time zone

我使用Simpledateformatter。但不能正确显示..请帮助

I used Simpledateformatter .. but not able to display correctly.. please help

推荐答案

使用 java.text.SimpleDateFormat 用于日期格式化从日期字符串到日期对象)。模式将为 yyyy-MM-dd HH:mm:ssZ

示例:

String dateStr = "2010-06-14 02:21:49-0400";
String pattern = "yyyy-MM-dd HH:mm:ssZ";
SimpleDateFormat sdf = new SimpleDateFormat(pattern);
Date date = sdf.parse(dateStr);

PS 要使用TimeZone,您可以使用 TimeZone.getDefault()并将其添加到SimpleDateFormat。

PS To use TimeZone, you can use TimeZone.getDefault() and add it to SimpleDateFormat.

SimpleDateFormat sdf = new SimpleDateFormat(pattern);
sdf.setTimeZone(TimeZone.getDefault());
Date date = sdf.parse(dateStr);

这篇关于根据设备的时区显示日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆