如何从String获取Date对象 [英] How to get a Date object from String

查看:117
本文介绍了如何从String获取Date对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

DateFormat formatter = new SimpleDateFormat("MM/dd/yyyy' 'HH:mm:ss");
Date d = (Date)formatter.parse(dateTime);
System.out.println("date in controller "+d);

我的输出为


日期在控制员星期一12月31日16:04:57 IST 2012

date in controller Mon Dec 31 16:04:57 IST 2012

请建议一种方法来输出日期在 MM / dd / yyyy HH:mm:ss 格式。
需要日期格式的输出,而不是字符串,以便将输出存储在mysql db中的 datetime 字段中

Please suggest a method to output the date in MM/dd/yyyy HH:mm:ss format. Need the output in date format and not as string so as to store the output in datetime field in mysql db

推荐答案

Need the output in date format and not as string so as to store the output in datetime field in mysql db

语句后

Date d = (Date)formatter.parse(dateTime);
java.sql.Date sqldate = new java.sql.Date(d.getTime())

你有一个 java.util.Date 对象和可以存储在mysql DB (列类型:datetime)

you have got a java.util.Date object and you can store it as it is in mysql DB (column type : datetime).

但是,当您打印 d 时,它默认为 .toString( )实现。我想你希望一些输出像Date @,但是toString以用户可读的格式打印,这就是为什么混乱。

However, when you are printing d, it defaults to the .toString() implementation. I think you expected some output like Date@ but the toString printed in user readable format thats why the confusion.

这篇关于如何从String获取Date对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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