使用DateTimeFormatterBuilder将字符串解析为LocalDateTime时如何防止自动生成的"T"字母 [英] How to prevent auto-generated 'T' letter when parsing String to LocalDateTime using DateTimeFormatterBuilder

查看:1005
本文介绍了使用DateTimeFormatterBuilder将字符串解析为LocalDateTime时如何防止自动生成的"T"字母的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这段代码,可以将String双向解析为LocalDateTime:

I have this fragment of code to bidirectional parsing String to LocalDateTime:

public class ConvertLocalDateToString {

private static final DateTimeFormatter CUSTOM_LOCAL_DATE;
static {
    CUSTOM_LOCAL_DATE = new DateTimeFormatterBuilder()
            .appendValue(YEAR, 4, 10, SignStyle.EXCEEDS_PAD)
            .appendLiteral('-')
            .appendValue(MONTH_OF_YEAR, 2)
            .appendLiteral('-')
            .appendValue(DAY_OF_MONTH, 2)
            .appendLiteral(' ')
            .appendValue(HOUR_OF_DAY, 2)
            .appendLiteral(':')
            .appendValue(MINUTE_OF_HOUR, 2)
            .optionalStart()
            .appendLiteral(':')
            .appendValue(SECOND_OF_MINUTE, 2)
            .toFormatter();
}

public static void main(String[] args) {

    String str = "2020-02-18 15:04:30";
    LocalDateTime dateTime = LocalDateTime.parse(str, CUSTOM_LOCAL_DATE); //2020-02-18T15:04:30

    LocalDateTime addedDateTime = LocalDateTime.parse(dateTime.plusHours(10).format(CUSTOM_LOCAL_DATE.withZone(ZoneOffset.UTC)));
    System.out.println(addedDateTime);
    System.out.println(dateTime); //2020-02-18T15:04:30

}

我的假设是'T'字母是由ISO-8601格式自动生成的.这导致:

My assumption is 'T' letter is auto-generated by ISO-8601 format. This caused:

DateTimeParseException:无法在索引10处解析文本'2020-02-18 15:04:30'

DateTimeParseException: Text '2020-02-18 15:04:30' could not be parsed at index 10

我如何摆脱它?

推荐答案

System.out.println(dateTime)System.out.println(String.valueOf(dateTime))相同.

String.valueOf(dateTime)dateTime.toString()相同,不同之处在于它可以处理null值.

String.valueOf(dateTime) is the same as dateTime.toString(), except it can handle null value.

LocalDateTime上调用toString()

Calling toString() on a LocalDateTime is documented to do this:

将此日期时间输出为字符串,例如2007-12-03T10:15:30.

输出将是以下ISO-8601格式之一:

The output will be one of the following ISO-8601 formats:

  • uuuu-MM-dd'T'HH:mm
  • uuuu-MM-dd'T'HH:mm:ss
  • uuuu-MM-dd'T'HH:mm:ss.SSS
  • uuuu-MM-dd'T'HH:mm:ss.SSSSSS
  • uuuu-MM-dd'T'HH:mm:ss.SSSSSSSSS

使用的格式将是最短的格式,用于输出被省略部分隐含为零的时间的完整值.

The format used will be the shortest that outputs the full value of the time where the omitted parts are implied to be zero.

如果要使用其他格式,请调用

If you want different format, call format(DateTimeFormatter formatter).

因此,回答您的问题:

我的假设是'T'字母由ISO-8601格式自动生成.

T不会比-:字符自动生成.

The T is no more auto-generated than the - and : characters.

我如何摆脱它?

致电format(...)并指定所需的格式,例如

Call format(...) and specify the format you want, e.g.

System.out.println(dateTime.format(DateTimeFormatter.ofPattern("uuuu-MM-dd HH:mm:ss")));

这篇关于使用DateTimeFormatterBuilder将字符串解析为LocalDateTime时如何防止自动生成的"T"字母的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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