gpt4 book ai didi

java - joda 日期 DateTimeFormatterBuilder 追加不起作用

转载 作者:行者123 更新时间:2023-11-29 05:23:16 25 4
gpt4 key购买 nike

先写代码。

@Test
public void testDateFormat()
{
// this is ok
DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss").parseDateTime("2014-5-15 20:00:00");
DateTimeFormat.forPattern("yyyy-MM-dd").parseDateTime("2014-5-15");

DateTimeFormatter formatter = new DateTimeFormatterBuilder()
.appendPattern("yyyy-MM-dd HH:mm:ss")
.appendPattern("yyyy-MM-dd")
// ues these way is also failed.
//.append(DateTimeFormat.forPattern("xxxx"))
//.append(DateTimeFormat.forPattern("xxxx").getParser())
.toFormatter();

try
{
formatter.parseLocalDateTime("2014-5-15 20:00:00");
}catch (Exception ignored){System.out.println("Failed A");}
try
{
formatter.parseLocalDateTime("2014-05-15 20:00:00");
}catch (Exception ignored){System.out.println("Failed B");}
try
{
formatter.parseLocalDateTime("2014-5-15");
}catch (Exception ignored){System.out.println("Failed C");}
try
{
formatter.parseLocalDateTime("2014-05-15");
}catch (Exception ignored){System.out.println("Failed D");}
}

输出

Failed A:Invalid format: "2014-5-15 20:00:00" is too short
Failed B:Invalid format: "2014-05-15 20:00:00" is too short
Failed C:Invalid format: "2014-5-15" is too short
Failed D:Invalid format: "2014-05-15" is too short

一切都失败了。难道我做错了什么?看起来应该没问题。

最佳答案

找到 this

正确的做法。

    DateTimeFormatter formatter = new DateTimeFormatterBuilder()
.append(null,
new DateTimeParser[]{DateTimeFormat.forPattern("yyyy-MM-dd").getParser(),
DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss").getParser()})
.toFormatter();

关于java - joda 日期 DateTimeFormatterBuilder 追加不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23801517/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com