如何将XML date(string)转换为Date? [英] How to convert XML date (string) to Date?

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

问题描述

我正在解析XML文件。在这个文件中有一个包含日期字符串2008-11-10T05:51:33Z的标签,我想将这个字符串转换为 java。 util.Date对象。



如何才能完成?

解决方案

使用 java.text .DateFormat - 或者更有可能是 SimpleDateFormat



或者,去 Joda时间与其无限更好的API。注意Java内置的API - DateFormats不是线程安全的。 (他们在Joda时间,几乎在任何地方使用不变的类型。)



An(未测试 - 应该是正常的,除了可能时区位)例子为Joda Time API:

  DateTimeFormatter fmt = DateTimeFormat.forPattern(yyyyMMdd'T'HH:mm:ssZ ); 
DateTime dt = fmt.parse(2008-11-10T05:51:33Z);


I am parsing XML file. In this file there is one tag containing date string "2008-11-10T05:51:33Z" and I want convert this string in to java.util.Date object.

How can this be done?

解决方案

Use java.text.DateFormat - or more likely, SimpleDateFormat.

Alternatively, go for Joda Time with its infinitely better API. Be careful with the Java built-in APIs - DateFormats aren't thread-safe. (They are in Joda Time, which uses immutable types almost everywhere.)

An (untested - should be fine except for possibly the timezone bit) example for the Joda Time API:

DateTimeFormatter fmt = DateTimeFormat.forPattern("yyyyMMdd'T'HH:mm:ssZ");
DateTime dt = fmt.parse("2008-11-10T05:51:33Z");

这篇关于如何将XML date(string)转换为Date?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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