如何使用可选的秒参数创建DateTimeformatter [英] How to create DateTimeformatter with optional seconds arguments

查看:229
本文介绍了如何使用可选的秒参数创建DateTimeformatter的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个 DateTimeformatter 来验证以下日期时间:

I am trying to create a DateTimeformatter to validate following date times:

String date1 = "2017-07-06T17:25:28";
String date2 = "2017-07-06T17:25:28.1";
String date3 = "2017-07-06T17:25:28.12";
String date4 = "2017-07-06T17:25:28.123";
String date5 = "2017-07-06T17:25:28.1234";
String date6 = "2017-07-06T17:25:28.12345";
String date7 = "2017-07-06T17:25:28.123456";
String date8 = "2017-07-06T17:25:28.";

我已尝试使用以下日期时间格式器来验证上述日期:

I have tried the following date time formatter to validate above dates:

public static final String DATE_TIME_FORMAT_PATTERN = "yyyy-MM-dd'T'HH:mm:ss";
DateTimeFormatter formatter1 = new DateTimeFormatterBuilder()
                                   .appendPattern(DATE_TIME_FORMAT_PATTERN)
                                   .appendFraction(ChronoField.MICRO_OF_SECOND, 0, 6, true)
                                   .toFormatter();

它适用于所有上述日期,但根据我的要求,它应该以<$ c失败$ c> java.time.format.DateTimeParseException for date8

It works fine for all the above dates, but according to my requirement it should fail with java.time.format.DateTimeParseException for date8.

注意:我知道我可以通过以下格式化程序实现预期的结果:

Note: I am aware that I can achieve expected result with following formatter:

DateTimeFormatter formatter2 = DateTimeFormatter
                       .ofPattern("yyyy-MM-dd'T'HH:mm:ss[.SSSSSS][.SSSSS][.SSSS][.SSS][.SS][.S]");

但我想知道我们可以通过更改 formatter1来达到预期的结果

But I wanted to know that can we achieve expected result by changing in formatter1?

用于解析我使用的日期:

For parsing the date I am using following:

LocalDateTime.parse(date1, formatter1);


推荐答案

您必须创建一个可选部分(使用 optionalStart() optionalEnd()方法)包含小数点后跟1到6位数字:

You must create an optional section (using optionalStart() and optionalEnd() methods) containing the decimal point followed by 1 to 6 digits:

String DATE_TIME_FORMAT_PATTERN = "yyyy-MM-dd'T'HH:mm:ss";
DateTimeFormatter formatter1 = new DateTimeFormatterBuilder()
    .appendPattern(DATE_TIME_FORMAT_PATTERN)
    // optional decimal point followed by 1 to 6 digits
    .optionalStart()
    .appendPattern(".")
    .appendFraction(ChronoField.MICRO_OF_SECOND, 1, 6, false)
    .optionalEnd()
    .toFormatter();

这从 date1 解析为 date7 并使用 date8 抛出 java.time.format.DateTimeParseException

This parses from date1 to date7 and throws a java.time.format.DateTimeParseException with date8.

这也有同样的作用:

String DATE_TIME_FORMAT_PATTERN = "yyyy-MM-dd'T'HH:mm:ss";
DateTimeFormatter formatter1 = new DateTimeFormatterBuilder()
    .appendPattern(DATE_TIME_FORMAT_PATTERN)
    // optional decimal point followed by 1 to 6 digits
    .optionalStart()
    .appendFraction(ChronoField.MICRO_OF_SECOND, 1, 6, true)
    .optionalEnd()
    .toFormatter();

这篇关于如何使用可选的秒参数创建DateTimeformatter的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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