本文介绍了year 和 year-of-era 和有什么不一样?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

DateTimeFormatter 类文档为年份和 y 年代定义了单独的符号 u:https://docs.oracle.com/javase/8/docs/api/java/time/格式/DateTimeFormatter.html#patterns

The DateTimeFormatter class documentation defines separate symbols u for year and y year-of-era: https://docs.oracle.com/javase/8/docs/api/java/time/format/DateTimeFormatter.html#patterns

年份和年代有什么区别?

What is the difference between year and year-of-era?

推荐答案

答案在 IsoChronology

  • 时代 - 有两个时代,当前时代"(CE) 和当前时代之前"(BCE).
  • year-of-era - 纪元与当前 CE 时代的预测年相同.对于 ISO 时代之前的 BCE 时代,随着时间的推移,年份从 1 向上增加.
  • proleptic-year - 预测年与当前时代的时代年相同.对于上一个时代,年份为零,然后为负值.

u 将为您提供生育年.y 会给你那个时代的年份.

u will give you the proleptic year.y will give you the year of the era.

区别主要是在公元前时代.预兆年 0 实际上是公元前 1 年,紧随其后的是预兆年 1,即公元 1 年.预兆年可以是负数,纪元不能.

The difference is mainly important for years of the BC era. The proleptic year 0 is actually 1 BC, it is followed by proleptic year 1 which is 1 AD. The proleptic year can be negative, the year of era can not.

这是一个有助于形象化它的工作原理的片段:

Here is a snippet that will help visualize how it works :

DateTimeFormatter formatter = DateTimeFormatter.ofPattern("'proleptic' : u '= era:' y G");

for (int i = 5; i > -6 ; i--) {
    LocalDate localDate = LocalDate.of(i, 3, 14);
    System.out.println(formatter.format(localDate));
}

输出:

proleptic : 5 = era: 5 AD
proleptic : 4 = era: 4 AD
proleptic : 3 = era: 3 AD
proleptic : 2 = era: 2 AD
proleptic : 1 = era: 1 AD
proleptic : 0 = era: 1 BC
proleptic : -1 = era: 2 BC
proleptic : -2 = era: 3 BC
proleptic : -3 = era: 4 BC
proleptic : -4 = era: 5 BC
proleptic : -5 = era: 6 BC

这篇关于year 和 year-of-era 和有什么不一样?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-30 18:49