【问题标题】:Display date according to the time zone of device根据设备时区显示日期
【发布时间】:2010-11-30 09:33:02
【问题描述】:

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

以上是来自我的网络服务的字符串... 有没有办法根据本地机器时区将此字符串转换为日期

我使用了 Simpledateformatter .. 但无法正确显示.. 请帮助

【问题讨论】:

    标签: java android datetime


    【解决方案1】:

    使用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。

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

    【讨论】:

    • 字符串模式 = "yyyy-MM-dd HH:mm:ssZ";SimpleDateFormat sdf = new SimpleDateFormat(pattern); sdf.setTimeZone(TimeZone.getDefault());日期日期 = sdf.parse(dateStr);输出 -- Wed Mar 31 14:37:33... 但我想要 2010-03-31 02:37 PM
    • @xydev,这不是你最初问的(is there a way to convert this string to the date according to the local machine time zone)。您能否进一步更新您的问题,以便我们确切知道您想要什么?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-19
    • 1970-01-01
    • 2022-01-03
    • 1970-01-01
    • 2011-08-07
    • 2020-12-26
    相关资源
    最近更新 更多