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

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

问题描述

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

当我使用该格式检索日期时,我会得到每一件事物
除外小时。小时总是 00 。这是我的输出:

 字符串日期---> 29-Apr-2010,13:00:14 PM 
交割日期之后---> 1272479414000 - Thu Apr 29 00:00:14 GMT + 05:30 2010

这是代码:

 日期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。



然而,这对我来说很好:

  String testDate =2010年4月29日13:00:14 
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。上述代码在系统上打印?如果它有效,你可以将你的例子缩小到显示问题的工作吗?


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

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

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

Here is the code:

    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);

What am I doing wrong?

解决方案

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);

It prints "Thu Apr 29 13:00:14 CEST 2010". What does the above code print on your system? If it works, can you reduce your example to a working one that shows the problem?

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

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