将String转换为java.util.Date [英] converting a String to java.util.Date

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

问题描述

我有一个字符串作为

strikedate="2011-11-19T00:00:00.000-05:00"

我需要把它放入一个java.util.Date
有没有人可以告诉我将此字符串转换为日期

I need to put this into a java.util.Date Could anybody please tell me how to convert this String to Date

推荐答案

当然 - 使用 SimpleDateFormat

Sure - use SimpleDateFormat.

DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ", Locale.US);
Date parsed = sdf.parse(text);

编辑:Eek - 这不是那么简单,因为你的时区格式。以上将不会使用 冒号。您可能需要使用 Joda时间来解析 冒号的版本。检查现在...

Eek - it's not quite that simple, due to your time zone format. The above will work without the colon. You may need to use Joda Time to parse the version with the colon. Checking that now...

编辑:是的,与Joda时间可以使用:

Yes, with Joda Time you can use:

DateTimeFormatter formatter = ISODateTimeFormat.dateTime();
DateTime parsed = formatter.parseDateTime(text);
// If you really need it
Date date = parsed.toDate();

Joda时间也是一个更好的日期/时间API开始 - 我强烈建议您可以使用它。

Joda Time is also a much nicer date/time API to start with - I'd strongly encourage you to use it if you can.

这篇关于将String转换为java.util.Date的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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