简体   繁体   English

在Java 8中格式化LocalDateTime时,默认的DateTimeFormatter返回空字符串

[英]Default DateTimeFormatter returns empty string when formatting LocalDateTime in Java 8

The following code returns an empty string: 以下代码返回一个空字符串:

LocalDateTime.parse("2018-01-01T00:00:00.00")
    .format(new DateTimeFormatterBuilder().toFormatter())

I'd expect either an exception or the date formatted in some default way. 我希望异常或以某种默认方式格式化的日期。

Have I found a bug in Java or this is as per specification? 我是否发现Java中的错误,或者是否符合规范? I cannot find any information in Javadoc about this behaviour. 我在Javadoc中找不到有关此行为的任何信息。

DateTimeFormatterBuilder is for building a format, it starts off empty. DateTimeFormatterBuilder用于构建格式,它从空开始。 You have to call its various methods such as appendPattern to add the format you want. 您必须调用其各种方法,例如appendPattern来添加所需的格式。

DateTimeFormatter has some standard formats you can use straight off. DateTimeFormatter具有一些可以直接使用的标准格式。 These use DateTimeFormatterBuilder to build the format. 它们使用DateTimeFormatterBuilder构建格式。 For example: 例如:

public static final DateTimeFormatter ISO_LOCAL_DATE;
static {
    ISO_LOCAL_DATE = new DateTimeFormatterBuilder()
            .appendValue(YEAR, 4, 10, SignStyle.EXCEEDS_PAD)
            .appendLiteral('-')
            .appendValue(MONTH_OF_YEAR, 2)
            .appendLiteral('-')
            .appendValue(DAY_OF_MONTH, 2)
            .toFormatter(ResolverStyle.STRICT, IsoChronology.INSTANCE);
}

Your code is equivalent to: 您的代码等效于:

LocalDateTime.parse("2018-01-01T00:00:00.00").format(DateTimeFormatter.ofPattern(""))

that produces an empty string in all cases. 在所有情况下都会产生一个空字符串。

I'd expect either exception or the date formatted in some default way 我希望异常或以某种默认方式格式化的日期

That's your opinion. 那是你的意见。 But IMO, creating an empty builder and add nothing to it should result in a - guess what - "empty" formatter - or a formatter with an empty pattern, or whatever. 但是,IMO,创建一个空的生成器并且不添加任何内容 ,应该会导致-猜猜是什么-“空”格式化程序-或具有空模式的格式化程序,或者其他原因。

The documentation doesn't explicity say this will happen, but it also doesn't say it'll throw an exception or format in some default way . 文档没有明确说明会发生这种情况,但是也没有说它将以某种默认方式抛出异常或格式 And if the docs don't say that, so why on earth would you expect that? 如果文档没有这么说,那么您为什么会期望呢? Based on what? 根据什么?

But the worst thing is thinking that, just because your based-on-nothing expectations weren't satisfied, then it's obviously a bug! 但是最糟糕的事情是,仅仅因为您对毫无根据的期望而感到不满意,这显然是一个错误! Why developers - specially the crappy ones - tend to think "wow, I found a bug" when things doesn't go as expected, when most of the time it's their fault? 为什么开发人员-尤其是cr脚的开发人员-在事情没有按预期进行时(大多数情况下都是他们的错)倾向于“哇,我发现了一个错误”

And why are you creating an empty pattern in the first place? 以及为什么首先要创建一个空模式?

"OMG, things didn't happen as I expected, so certainly it's a bug!" “ OMG,事情没有按我预期的那样发生,所以肯定是一个错误!”

-- crappy developer -cr脚的开发人员

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM