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

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

问题描述

我有一个字符串,该字符串是通过调用Date类的实例的 toString 方法获得的.如何从该字符串获取Date对象?

I have a string obtained by calling the toString method of an 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 = ...

更新

我尝试使用SimpleDateFormat,但得到 java.text.ParseException:无法解析的日期Date.toString()产生的日期格式是什么?

I've tried to use SimpleDateFormat, but I get java.text.ParseException: Unparseable date What is the date format produced by Date.toString ()?

推荐答案

如果您的真正目标是为某种定制的持久性或数据传输序列化 Date 对象,则一种简单的解决方案是:

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转换回Date?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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