从toString()解析Java日期 [英] Parsing a java Date back from toString()
问题描述
我有一个String
,其中包含在java.util.Date
实例上调用的toString()
的结果.如何将这个值解析回Date
对象?
I have a String
containing the result of toString()
called on an instance of java.util.Date
. How can I parse this value back to a Date
object?
Java文档说toString()
将此Date
对象转换为以下形式的String
:
The Java docs say that toString()
converts this Date
object to a String
of the form:
dow mon dd hh:mm:ss zzz yyyy
,但是当然没有"dow"
或"mon"
这样的格式标签.
but of course there is no such format tag as "dow"
or "mon"
.
有人可以帮助我解决这个问题.请注意,不幸的是toString()
调用是在我无法控制的一段代码中.
Could someone please help me with this problem. Please note that unfortunately the toString()
call is in a piece of code out of my control.
推荐答案
如果您无法控制生成字符串的代码:
要解析toString()
格式,您需要将SimpleDateFormat
语言环境设置为英语,并使用以下格式:"EEE MMM dd HH:mm:ss Z yyyy"
.
To parse the toString()
format you need to set the SimpleDateFormat
locale to english and use the format: "EEE MMM dd HH:mm:ss Z yyyy"
.
SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd HH:mm:ss Z yyyy", new Locale("us"));`
这篇关于从toString()解析Java日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!