从toString()解析Java日期 [英] Parsing a java Date back from toString()

查看:82
本文介绍了从toString()解析Java日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个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屋!

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