简单的日期格式给错误的时间 [英] Simple date formatting giving wrong time

查看:156
本文介绍了简单的日期格式给错误的时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图解析字符串到日期,它给了我正确的日期,其中的时间是错误的。

I am trying to parse a String to a Date and it giving me right date where as time is wrong.

SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm aaa");

try {

    Date date = formatter.parse("2015-08-20 05:00 AM");
    Log.e("date", date+"");           // Logcat printing Thu Aug 20 00:00:00 GMT+05:30 2015
} catch (ParseException e) {
    Log.e("Error",e.toString());
}

正如你可以看到不论在任何时间,每次解析日期时间显示 00:00:00 我要的是周四8月20日05:00:00 GMT + 05:30 2015年

推荐答案

看来,问题是,你的模式String指定的AM / PM,但使用大写 ^ h S为每小时字符。这表明一个24小时制,这显然不使用AM / PM。更改时数字符转换为小写 ^ h 的,这表明小时AM / PM(0-11)。

It seems the problem was that your pattern String specified am/pm, but was using uppercase H's for the hour characters. These indicate a 24-hour clock, which obviously doesn't use am/pm. Change the hour characters to lowercase h's, which indicate the hour in am/pm (0-11).

SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd hh:mm aaa");

借助文档的的SimpleDateFormat 解释各种可接受的模式字符。

The docs for SimpleDateFormat explain the various acceptable pattern characters.

这篇关于简单的日期格式给错误的时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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