Joda时间:如何将String转换为LocalDate? [英] Joda time : How to convert String to LocalDate?

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

问题描述

如何指定格式字符串以仅从字符串转换日期。就我而言,只有日期部分是相关的

How to specify the format string to convert the date alone from string. In my case, only the date part is relevant

将其构造为 DateTime 失败:

String dateString = "2009-04-17";
DateTimeFormatter formatter = DateTimeFormat.forPattern("yyyy-MM-dd");
DateTime dateTime = formatter.parseDateTime(dateString);

错误 java.lang.IllegalArgumentException:格式无效:2011-04 -17太短

可能是因为我应该使用 LocalDate 。但是,我没有看到 LocalDate 的任何格式化程序。将 String dateString =2009-04-17; 转换为 LocalDate (或其他内容)的最佳方法是什么?如果那不是正确的代表)

Probably because I should use LocalDate instead. But, I do not see any formatter for LocalDate . What is the best way to convert String dateString = "2009-04-17"; into LocalDate (or something else if that is not the right representation)

谢谢......

推荐答案

<你可能正在寻找 LOCALDATE(对象) 。这有点令人困惑,因为它需要一个通用的 Object ,但文档表明它将使用 ConverterManager 知道如何处理 String 如果你将 String 传递给构造函数,例如

You're probably looking for LocalDate(Object). It's a bit confusing since it takes a generic Object, but the docs indicate that it will use a ConverterManager that knows how to handle a String if you pass a String to the constructor, e.g.

LocalDate myDate = new LocalDate("2010-04-28");

这篇关于Joda时间:如何将String转换为LocalDate?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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