如何将Date.toString转换回日期? [英] How to convert Date.toString back to Date?

查看:623
本文介绍了如何将Date.toString转换回日期?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个通过调用Date类实例的 toString 方法获得的字符串。

如何从这个字符串中获取Date对象?

I have a string obtained by calling the toString method of instance of the class Date.
How can I get a Date object from this string?

Date d = new Date();
String s = d.toString;
Date theSameDate = ...

UPDATE

我试过使用SimpleDateFormat,但我得到 java.text.ParseException:无法解析的日期
你能告诉我Date生成的日期格式吗? .toString()?

UPDATE
I've tried to use SimpleDateFormat, but I get java.text.ParseException: Unparseable date Can you tell me the date format produced by Date.toString ()?

推荐答案

如果您的真正目标是序列化日期某种自定义持久性或数据传输的对象,一个简单的解决方案是:

If your real goal is to serialize a Date object for some kind of custom made persistence or data transfer, a simple solution would be:

Date d = new Date();
long l = d.getTime();
Date theSameDate = new Date(l);

这篇关于如何将Date.toString转换回日期?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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