Java Joda 时间:如何将 String 转换为 LocalDate?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2721614/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 12:06:06  来源:igfitidea点击:

Joda time : How to convert String to LocalDate?

javajodatime

提问by bsr

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

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

Constructing it as DateTimefails:

将其构建为DateTime失败:

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

with error java.lang.IllegalArgumentException: Invalid format: "2011-04-17" is too short

有错误 java.lang.IllegalArgumentException: Invalid format: "2011-04-17" is too short

Probably because I should use LocalDateinstead. 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。但是,我没有看到任何格式化程序LocalDate。转换String dateString = "2009-04-17";为的最佳方式是什么LocalDate(或其他方式,如果这不是正确的表示方式)

thanks...

谢谢...

采纳答案by Hank Gay

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 ConverterManagerthat knows how to handle a Stringif you pass a Stringto the constructor, e.g.

您可能正在寻找LocalDate(Object). 这有点令人困惑,因为它需要一个 generic Object,但是文档表明如果将 a传递给构造函数,它将使用ConverterManager知道如何处理 a 的Stringa String,例如

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

回答by JodaStephen

Use the parse(String)method.

使用parse(String)方法。

LocalDate date = LocalDate.parse("2009-04-17");

回答by CharlesF

This worked for me:

这对我有用:

LocalDate d1 = LocalDate.parse("2014-07-19");
LocalDate dNow = LocalDate.now();  // Current date

回答by Computered

You can use LocalDate.ofwith the year, month and day passed as separate arguments:

您可以使用LocalDate.of作为单独参数传递的年、月和日:

LocalDate date1 = LocalDate.of(2009, 4, 17);
LocalDate date2 = LocalDate.of(2009, Month.APRIL, 17);

回答by Kevin Won

There's a somewhat subtle bug-ish issue with using LocalDate.parse()or new LocalDate(). A code snippet is worth a thousand words. In the following example in the scala repl I wanted to get a Local date in a string format yyyyMMdd. LocalDate.parse()is happy to give me an instance of a LocalDate, but it's not the correct one (new LocalDate()has the same behavior):

使用LocalDate.parse()or有一个有点微妙的错误问题new LocalDate()。一个代码片段值一千字。在下面的 scala repl 示例中,我想以字符串格式 yyyyMMdd 获取本地日期。LocalDate.parse()很高兴给我一个 LocalDate 的实例,但它不是正确的(new LocalDate()具有相同的行为):

scala> org.joda.time.LocalDate.parse("20120202")
res3: org.joda.time.LocalDate = 20120202-01-01

I give it Feb 2, 2016 in the format yyyyMMdd and I get back a date of January 1, 20120202. I'm going out on a limb here: I don't think that this is what it should be doing. Joda uses 'yyyy-MM-dd' as the default but implicitly will accept a string with no '-' characters, thinking you want January 1 of that year? That does not seem like a reasonable default behavior to me.

我给它 2016 年 2 月 2 日,格式为 yyyyMMdd,我得到的日期是 20120202 年 1 月 1 日。我在这里有点犹豫:我认为这不是它应该做的。Joda 使用 'yyyy-MM-dd' 作为默认值,但会隐式地接受没有 '-' 字符的字符串,认为您想要当年的 1 月 1 日?对我来说,这似乎不是一个合理的默认行为。

Given this, it seems to me that using a joda date formatter that can't be so easily fooled is a better solution to parsing a string. Moreover, LocalDate.parse()should probably throw an exception if the date format is not 'yyyy-MM-dd':

鉴于此,在我看来,使用不易被愚弄的 joda 日期格式化程序是解析字符串的更好解决方案。此外,LocalDate.parse()如果日期格式不是“yyyy-MM-dd” ,则可能应该抛出异常:

scala> val format = org.joda.time.format.DateTimeFormat.forPattern("yyyyMMdd")
format: org.joda.time.format.DateTimeFormatter = org.joda.time.format.DateTimeFormatter@56826a75

scala> org.joda.time.LocalDate.parse("20120202", format)
res4: org.joda.time.LocalDate = 2012-02-02

this will cause other formats to fail so you don't get this odd buggy behavior:

这将导致其他格式失败,因此您不会遇到这种奇怪的错误行为:

scala> val format = org.joda.time.format.DateTimeFormat.forPattern("yyyy-MM-dd")
format: org.joda.time.format.DateTimeFormatter = org.joda.time.format.DateTimeFormatter@781aff8b

scala> org.joda.time.LocalDate.parse("20120202", format)
java.lang.IllegalArgumentException: Invalid format: "20120202" is too short
  at org.joda.time.format.DateTimeFormatter.parseLocalDateTime(DateTimeFormatter.java:900)
  at org.joda.time.format.DateTimeFormatter.parseLocalDate(DateTimeFormatter.java:844)
  at org.joda.time.LocalDate.parse(LocalDate.java:179)
  ... 65 elided

which is a much more sane behavior than returning a date in the year 20120202.

这比返回 20120202 年的日期要理智得多。

回答by Mikhail Batcer

In my case, incoming string may be in one of two formats. So I first try to parse string with more specific format:

就我而言,传入的字符串可能是两种格式之一。所以我首先尝试用更具体的格式解析字符串:

String s = "2016-02-12";
LocalDateTime ldt;
try {
    ldt = LocalDateTime.parse(s, DateTimeFormat.forPattern("YYYY-MM-dd HH:mm"));
}
catch (IllegalArgumentException e) {
    ldt = LocalDateTime.parse(s, DateTimeFormat.forPattern("YYYY-MM-dd"));
}