Tag: 2 digit year

在java.time中从`MonthDay`或`YearMonth`类本地化字符串?

Java 8及更高版本中内置的java.time类提供MonthDay和YearMonth类。 他们的toString和parse方法使用标准的ISO 8601格式( YYYY-MM –MM-DD和YYYY-MM ),这是明智的。 对于向人类呈现,标准格式可能不合适。 反正是否有生成自动本地化的字符串来表示MonthDay或YearMonth对象中的值? 例如,在美国,用户通常可能需要月/日的MM / DD和年/月的MM / YY。 在英国,用户可能需要DD / MM的月份。 无论如何,通过Locale自动化这些变化而不是明确定义格式模式? 我使用面向日期的本地化格式化程序尝试了以下代码。 Locale l = Locale.US; DateTimeFormatter f = DateTimeFormatter.ofLocalizedDate ( FormatStyle.SHORT ).withLocale ( l ); YearMonth ym = YearMonth.of ( 2017 , Month.JANUARY ); MonthDay md = MonthDay.of ( Month.JANUARY , 29 ); String outputYm = ym.format […]

强制从java.time中的`DateTimeFormatter.ofLocalized …`生成的本地化字符串中的4位数年份

java.time中的DateTimeFormatter类提供了三种ofLocalized…方法,用于生成表示包含一年的值的字符串。 例如, ofLocalizedDate 。 Locale l = Locale.US ; DateTimeFormatter f = DateTimeFormatter.ofLocalizedDate( FormatStyle.SHORT ).withLocale( l ); LocalDate today = LocalDate.now( ZoneId.of( “America/Chicago” ) ); String output = today.format( f ); 对于我看过的语言环境,年份只是较短的FormatStyle样式中的两位数。 如何让java.time本地化却迫使年份变为四位数而不是两位数 ? 我怀疑答案在于DateTimeFormatterBuilder类。 但我找不到任何改变一年的长度的function。 我也仔细阅读了Java 9源代码 ,但是不能很好地解释代码以找到答案。 这个问题类似于: 在java的simpledateformat中强制使用4位数年份 Jodatime:如何打印4位数年份? …但是这些问题针对的是现在由java.time类取代的旧日期时间框架。

将字符串解析为本地日期不会使用所需的世纪

我正在使用这个DateTimeFormatter: DateTimeFormatter.ofPattern(“ddMMYY”) 我想解析字符串150790 ,我收到此错误: Unable to obtain LocalDate from TemporalAccessor: {DayOfMonth=15, MonthOfYear=7, WeekBasedYear[WeekFields[MONDAY,4]]=2090},ISO of type java.time.format.Parsed 显然,我想获得以下TemporalAccessor : {DayOfMonth=15, MonthOfYear=7, WeekBasedYear=1990} 你知道我为什么得到2090而不是1990年? 谢谢你的帮助