使用Joda Time库将日期字符串转换为DateTime对象 [英] Converting a date string to a DateTime object using Joda Time library

查看:195
本文介绍了使用Joda Time库将日期字符串转换为DateTime对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我以日期作为字符串,格式如下:04/02/2011 20:27:05。我正在使用Joda-Time库,并希望将其转换为 DateTime 对象。我做了:

  DateTime dt = new DateTime(04/02/2011 20:27:05)

但是我收到以下错误:

 格式无效:04/02/2011 14:42:17在/ 02/2011 14:42:17格式错误
pre>

如何将上述日期转换为 DateTime 对象?

解决方案

使用 DateTimeFormat

  DateTimeFormatter formatter = DateTimeFormat.forPattern dd / MM / yyyy HH:mm:ss); 
DateTime dt = formatter.parseDateTime(string);


I have a date as a string in the following format "04/02/2011 20:27:05". I am using Joda-Time library and would like to convert it to DateTime object. I did:

DateTime dt = new DateTime("04/02/2011 20:27:05")

But I'm getting the following error :

Invalid format: "04/02/2011 14:42:17" is malformed at "/02/2011 14:42:17"

How to convert the above date to a DateTime object?

解决方案

Use DateTimeFormat:

DateTimeFormatter formatter = DateTimeFormat.forPattern("dd/MM/yyyy HH:mm:ss");
DateTime dt = formatter.parseDateTime(string);

这篇关于使用Joda Time库将日期字符串转换为DateTime对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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