如何使用DateTimeFormatter在Java8中处理yyyy-mm和yyyy [英] How to process yyyy-mm and yyyy in java8 using DateTimeFormatter

查看:679
本文介绍了如何使用DateTimeFormatter在Java8中处理yyyy-mm和yyyy的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用SimpleDateFormat格式化或验证日期,但是我想通过使用Java 8 DateTimeFormatter使其成为线程安全的.我无法达到某些要求.

Am using SimpleDateFormat to format or validate the dates, but I would like to make it thread-safe by using java 8 DateTimeFormatter. I am having trouble to achieve some requirement.

我的应用程序仅接受三种类型的格式. "yyyy-MM-dd","yyyy-MM","yyyy"

My application will accept only three types of formats. "yyyy-MM-dd", "yyyy-MM", "yyyy"

Existing Code gives me desired output:
/*simple date format to process yyyy-MM-dd format
SimpleDateFormat simpleDateFormat1 = new SimpleDateFormat("yyyy-MM-dd")
/*simple date format to process yyyy-MM format
SimpleDateFormat simpleDateFormat2 = new SimpleDateFormat("yyyy-MM")

/*simple date format to process yyyy format
SimpleDateFormat simpleDateFormat3 = new SimpleDateFormat("yyyy")

/* to parse input
simpleDateFormat.parse(input)
/* to format
simpleDateFormat.format(simpleDateFormat1)

这是输入和预期的输出:

Here is the input and expected output:

  input             expected
'2018-03-19'       '2018-03-19'
'2018-03'          '2018-03'
'2018'             '2018'
'2017-02-54'       '2017-02'
'2016-13-19'       '2016'

  1. 如何在Java 8 DateTimeForma enter code here tter中获得相同的结果?

  1. How can I achieve same result in java 8 DateTimeFormaenter code heretter?

/* Java 8日期时间格式化程序 DateTimeFormatter dateTimeFormatter =新的DateTimeFormatter("yyyy-MM-dd")

/* java 8 date time formatter DateTimeFormatter dateTimeFormatter = new DateTimeFormatter("yyyy-MM-dd")

当所有年份,月份和日期值都正确时,以上代码段才起作用.任何帮助将不胜感激.

The above snippet works when all year and month and date values are correct. Any help would be highly appreciated.

推荐答案

Am使用SimpleDateFormat格式化或验证日期

Am using SimpleDateFormat to format or validate the dates

请勿使用SimpleDateFormat.

与最早的Java版本捆绑在一起的可怕的日期时间类早在几年前就被java.time 类所取代/jsr/detail?id = 310"rel =" nofollow noreferrer> JSR 310 .

The terrible date-time classes bundled with the earliest versions of Java were years ago supplanted by the modern java.time classes defined in JSR 310.

使用Java 8 DateTimeFormatter

线程安全

thread-safe by using java 8 DateTimeFormatter

是的,与传统的日期时间类不同, java.time 类使用线程安全的.

Yes, unlike the legacy date-time classes, the java.time classes use immutable objects and are thread-safe by design.

这是输入和预期的输出:

Here is the input and expected output:

某些输入可以简单地通过它们的长度来检测.

Some of your inputs could be detected simply by their length.

// Ten-digits long, assume ISO 8601 date.
LocalDate ld = LocalDate.parse( "2018-03-19" ) ;

// Seven digits long, assume ISO 8601 year-month.
YearMonth ym = YearMonth.parse( "2018-03" ) ;

// Four digits, assume year.
Year y = Year.parse( "2018" ) ;

请注意,以上输入均符合 ISO 8601 .解析/生成字符串时, java.time 类默认使用ISO 8601格式.因此,无需指定格式化模式.因此,不需要显式的DateTimeFormatter对象.

Notice that the above inputs all comply with ISO 8601. The java.time classes use ISO 8601 formats by default when parsing/generating strings. So no need to specify a formatting pattern. And therefore no need for an explicit DateTimeFormatter object.

'2017-02-54''2017-02'

'2017-02-54' '2017-02'

这个例子使我感到困惑.如果您的意思是当遇到日期无效的月份中的日期时,仅使用年份和月份,而忽略该日期",我想您也许能够做到这一点.调查宽大" DateTimeFormatter上的模式.也许使用DateTimeFormatterBuilder来构建灵活的DateTimeFormatter.但坦率地说,我会拒绝此类数据作为错误的输入.产生可靠数据应该是数据的发布者的工作,而不是猜测错误数据背后的意图的消费者的工作.

This example puzzles me. If you mean "When encountering a date with invalid day-of-month, just use the year and month while ignoring the day", I suppose you might be able to do that. Look into "lenient" mode on a DateTimeFormatter. Perhaps use DateTimeFormatterBuilder to build a flexible DateTimeFormatter. But frankly, I would reject such data as faulty inputs. It should be the job of the publisher of the data to produce reliable data, not the job of the consumer to guess the intention behind faulty data.

预期输入

'2016-13-19''2016'

'2016-13-19' '2016'

再次尝试在我不会玩的危险游戏中猜测无效输入的有效部分.如果月份和日期无效,您如何知道年份有效?更糟糕的是,如果这些数据的发布者可以发出这样的错误数据,那么您如何知道看似有效的2018-03-19输入实际上是正确的呢?如果月份13是错误的,怎么知道月份为03的输入也不是错误?

Again, trying to guess the valid parts of invalid inputs in a dangerous game I would not play. If the month and day are invalid, how do you know the year is valid? Even worse, if the publisher of this data can emit such erroneous data, how do you know an apparently valid 2018-03-19 input is actually correct? If month 13 is a mistake, how do know an input with month of 03 is not a mistake too?

向发布商介绍有关 ISO 8601 标准的有问题的数据,并要求他们修复他们的错误.

Teach the publisher of this problematic data about the ISO 8601 standard, and ask them to fix their bugs.

这篇关于如何使用DateTimeFormatter在Java8中处理yyyy-mm和yyyy的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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