【问题标题】:Not getting proper time by using SimpleDateFormat使用 SimpleDateFormat 没有得到适当的时间
【发布时间】:2013-10-27 18:18:41
【问题描述】:

我正在尝试将日期从 UTC 格式的 JSON 转换为我的本地时间。

我的时间是10/27/2013 5:58:02 PM,我需要将其转换为我的当地时间,即+5:30

但我得到的是10/27/2013 6:28:02

我的代码是

    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("M/d/yyyy H:mm:ss a");
    SimpleDateFormat longDateFormat=new SimpleDateFormat("M/d/yyyy H:mm:ss");
    simpleDateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));

        String formattedDate="";
        try
        {
            Date myDate = simpleDateFormat.parse(mDateAndTime);

            formattedDate = longDateFormat.format(myDate);
        } catch (ParseException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

【问题讨论】:

  • 如果删除该行... simpleDateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));也许它有效..我不确定,因为我无法测试它。请试一试
  • 谢谢回复,但我得到了 10/27/2013 0:58:02

标签: android simpledateformat utc


【解决方案1】:

我认为这会对你有所帮助:

SimpleDateFormat simpleDateFormat = new SimpleDateFormat("M/d/yyyy hh:mm:ss a");
SimpleDateFormat longDateFormat = new SimpleDateFormat("M/d/yyyy HH:mm:ss z");
simpleDateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
try
{
    Date myDate = simpleDateFormat.parse("10/25/2013 02:00:00 PM");
    String now = simpleDateFormat.format(myDate.getTime());
    System.out.println("12 hour format : " + now);
    String time24 = longDateFormat.format(simpleDateFormat.parse(now));
    System.out.println("24 hour format : " + time24);
} catch (ParseException e)
{
    // TODO Auto-generated catch block
    e.printStackTrace();
}

【讨论】:

  • 不,我收到 10/27/2013 0:58:02 AM +0530。这里的“Z”是什么?
  • 感谢您的努力,但它不起作用。明天我会尝试别的东西
猜你喜欢
  • 2011-12-27
  • 1970-01-01
  • 1970-01-01
  • 2013-11-08
  • 1970-01-01
  • 2014-11-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多