简体   繁体   中英

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

I have a date in the Gregorian calendar and want to look up the same day in the Julian calendar.

This should be easy with the Date/Time API of Java 8 but I couldn't find a way to do it. The code should look something like this:

/*
 * 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);

You can try it out online here


Edit

This is certainly no duplicate of Convert from Gregorian to Julian calendar . My question asks specifically about the Java Data/Time API.

The Julian chronology is not built into Java 8 or 9. Instead, the ThreeTen-Extra project has JulianChronology and JulianDate classes. Using it is simple.

JulianDate jd = JulianDate.from(localIsoDate);

I rather think like @Hugo that your research efforts could have been better .

Does Java-8/9 support the Julian calendar?

Research method: Please study the official API.

The count of chronologies in OpenJDK is limited to just five (see the mentioned implementations). No chronology (ISO as proleptic gregorian, Hijri-Umalquara, Minguo, ThaiBuddhist, Japanese) supports the Julian calendar. Hence the simple answer is: No chance to convert to Julian calendar only using the Java-8/9-API .

Does Oracle plan to implement the Julian calendar?

Research method: Google query.

A simple search using Google with the keywords "openjdk julian calendar" shows as first 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.

This issue was submitted by the primary author of new java.time-API S. Colebourne already in year 2015. Oracle had got a lot of time to add the suggested finished implementation to Java-9, but missed it. Regarding the fact that Oracle has refused some other calendar systems (like Persian or Hebrew) or even thrown out existing chronologies (like Coptic which had been a part of OpenJDK-8 before release), I would not be very sure if Oracle is really going to add the Julian calendar to a later Java-version in context of java.time -API . Let's see.

Can we use other 3rd-party libraries to convert java.time.LocalDate ?

Research method: Google query.

A simple search using Google with the keywords "java julian calendar" show tons of possible solutions. Just have some patience to study at least the links of about first 10 search result pages:

Yes, we can.

  • If you are willing or know how to convert java.time.LocalDate to java.util.GregorianCalendar (tip: Use timezone to convert first to ZonedDateTime and then to old API): Set the gregorian cutover date to new Date(Long.MAX_VALUE) .

  • Or use Joda-Time: It offers a Julian chronology . Conversion example:

 LocalDate threeten = LocalDate.now(); org.joda.time.LocalDate joda = new DateTime( threeten.getYear(), threeten.getMonthValue(), threeten.getDayOfMonth(), 0, 0 ).withChronology(JulianChronology.getInstance()).toLocalDate(); 
 LocalDate threeten = LocalDate.now(); JulianCalendar jcal = PlainDate.from(threeten).transform(JulianCalendar.axis()); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM