如何使用 Java 8 Date/Time API 将公历日期转换为儒略日期?

How to convert a Gregorian date to Julian date with the Java 8 Date/Time API?

我在公历中有一个日期,想在儒略历中查找同一天。

使用 Date/Time API of Java 8 应该很容易,但我找不到方法。代码应如下所示:

/*
 * LocalDate uses the Gregorian calendar
 */
LocalDate date = LocalDate.parse("2017-10-29");


/*
 * switch from Gregorian calendar to Julian calendar here
 */
;


/*
 * output should be 2017-10-16
 */
System.out.println(date);

您可以在线试用here


编辑

这肯定不是重复的 Convert from Gregorian to Julian calendar。我的问题专门询问 Java Data/Time API.

Java 8 或 9 并未内置朱利安年表。取而代之的是 ThreeTen-Extra project has JulianChronology and JulianDate 类。使用起来很简单。

JulianDate jd = JulianDate.from(localIsoDate);

我和@Hugo 一样认为你的研究工作本可以做得更好

Java-8/9 支持儒略历吗?

研究方法:请研究官方API.

OpenJDK 中的时间顺序计数仅限于 five(请参阅上述实现)。没有年表(ISO 作为 proleptic gregorian、Hijri-Umalquara、Minguo、ThaiBuddhist、Japanese)支持儒略历。因此,简单的答案是:没有 转换为儒略历的机会 仅使用 Java-8/9-API.

Oracle 是否计划实施儒略历?

研究方法:Google查询。

使用 Google 和关键字 "openjdk julian calendar" 的简单搜索显示为第一个 link following issue:

The Julian calendar system was the primary calendar system in use before the current Gregorian/ISO one. Because of its widespread historic use, especially in the history/academic domain, the JDK should support it.

An implementation is available at ThreeTen-Extra. Note that this issue does not call for a calendar that can "cutover" between the Julian and Gregorian.

这个问题是由新 java.time-API S. Colebourne 的主要作者在 2015 年提交的。Oracle 有很多时间将建议的完成实现添加到 Java-9,但错过了。关于 Oracle 拒绝了其他一些日历系统(如波斯语或希伯来语)甚至抛弃了现有的年表(如科普特语,它在发布前已成为 OpenJDK-8 的一部分)这一事实,我不太确定如果 Oracle 真的要在 java.time-API 的上下文中将 Julian 日历添加到以后的 Java 版本中。让我们看看。

我们可以使用其他第 3 方库来转换 java.time.LocalDate 吗?

研究方法:Google查询。

使用 Google 和关键字 "java julian calendar" 的简单搜索显示了大量可能的解决方案。至少要有耐心研究大约前 10 个搜索结果页面的 link:

是的,我们可以。

  • 如果您愿意或知道如何将 java.time.LocalDate 转换为 java.util.GregorianCalendar(提示:使用时区先转换为 ZonedDateTime,然后再转换为旧的 API): Set the gregorian cutover datenew Date(Long.MAX_VALUE).

  • 或使用 Joda-Time:它提供了 Julian chronology。转换示例:

LocalDate threeten = LocalDate.now();
org.joda.time.LocalDate joda =
  new DateTime(
      threeten.getYear(),
      threeten.getMonthValue(),
      threeten.getDayOfMonth(),
      0,
      0
  ).withChronology(JulianChronology.getInstance()).toLocalDate();
  • 或使用Threeten-Extra (already suggested by S. Colebourne in the OpenJDK-issue and 也显示了简单的转换)。

  • 或者使用我的图书馆Time4J which offers even two different calendars: JulianCalendar and HistoricCalendar (latter one has a configurable ChronoHistory for the irregular Julian leap years between 44 BC and 8 AD, see the class AncientJulianLeapYears)。转换示例:

LocalDate threeten = LocalDate.now();
JulianCalendar jcal = PlainDate.from(threeten).transform(JulianCalendar.axis());