将字符串转换为 java.util.Date [英] Convert String to java.util.Date

查看:42
本文介绍了将字符串转换为 java.util.Date的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我以这种格式将日期存储在 SQLite 数据库中:

I am storing the dates in a SQLite database in this format:

d-MMM-yyyy,HH:mm:ss aaa

当我用那种格式检索日期时,一切都很好除了小时.小时总是 00.这是我的输出:

When I retrieve the date with that format I am get every thing fine except the hour. The hour is always 00. Here is my output:

String date--->29-Apr-2010,13:00:14 PM
After convrting Date--->1272479414000--Thu Apr 29 00:00:14 GMT+05:30 2010

代码如下:

    Date lScheduledDate = CalendarObj.getTime();
    DateFormat formatter = new SimpleDateFormat("d-MMM-yyyy,HH:mm:ss aaa");
    SomeClassObj.setTime(formatter.format(lScheduledDate));

    String lNextDate = SomeClassObj.getTime();
    DateFormat lFormatter = new SimpleDateFormat("d-MMM-yyyy,HH:mm:ss aaa");
    Date lNextDate = (Date)lFormatter.parse(lNextDate);
    System.out.println("output here"+lNextDate);

我做错了什么?

推荐答案

我认为您的日期格式没有意义.没有 13:00 PM.删除格式末尾的aaa"或将 HH 变成 hh.

I think your date format does not make sense. There is no 13:00 PM. Remove the "aaa" at the end of your format or turn the HH into hh.

尽管如此,这对我来说很好用:

Nevertheless, this works fine for me:

String testDate = "29-Apr-2010,13:00:14 PM";
DateFormat formatter = new SimpleDateFormat("d-MMM-yyyy,HH:mm:ss aaa");
Date date = formatter.parse(testDate);
System.out.println(date);

它会打印Thu Apr 29 13:00:14 CEST 2010".

It prints "Thu Apr 29 13:00:14 CEST 2010".

这篇关于将字符串转换为 java.util.Date的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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