7

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.

Paule
  • 138
  • 1
  • 9
  • Check this: https://coderanch.com/t/410264/java/Julian-Gregorian-date-conversion – Omar Mneimneh Oct 29 '17 at 14:30
  • 2
    Possible duplicate of [Convert from Gregorian to Julian calendar](https://stackoverflow.com/questions/46906227/convert-from-gregorian-to-julian-calendar) –  Oct 29 '17 at 14:41
  • 3
    @Hugo that's a very bad duplicate as neither the question nor the answers use the Java8 Java time API. And removing the [java] language tag in your edit was also a bad idea - java-8 is not a language tag. – Erwin Bolwidt Oct 29 '17 at 14:44
  • 2
    So, back on topic. I don't see why you can't take the logic from the other question up to the second creation of the Calendar object and replace it with a LocalDate – OneCricketeer Oct 29 '17 at 15:18
  • The code uses possibly unsafe conversion logic. I want to avoid this, if there is a better alternative. – Paule Oct 29 '17 at 15:42
  • Perhaps the [Julian chronology](http://www.threeten.org/threeten-extra/apidocs/org/threeten/extra/chrono/JulianChronology.html) defined the [ThreeTen-Extra](http://www.threeten.org/threeten-extra/) project might help, a suite of classes to complement java.time classes. – Basil Bourque Oct 29 '17 at 16:03
  • @Hugo I clearly stated *how* i would like to solve the problem in the question itself and in the text. For what additional purpose should i explain how i want the problem not to be solved? The question may seem like i show no effort but the opposite is true. For the sake of a useful question, I tried to eliminate everything that is not relevant to understanding or solving the problem. – Paule Oct 29 '17 at 17:50
  • 2
    You said you wanted to switch from gregorian to julian calendar in Java 8. The duplicate contains an algorithm that can be adapted to be used with Java 8 `LocalDate` - as you weren't specific, I thought it could be used. If you have said you didn't want to do the math by yourself and use a built-in API function instead, I wouldn't have suggested the duplicate. Anyway, if you don't want the question to look like it's no effort, then put your efforts in the question. We can't guess what you've tried if you don't tell us. –  Oct 29 '17 at 18:01

2 Answers2

5

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);
JodaStephen
  • 60,927
  • 15
  • 95
  • 117
0

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());
Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
Meno Hochschild
  • 42,708
  • 7
  • 104
  • 126
  • 1. I studied the API and saw no JulianChronology. But I didn't came to the conclusion that there is no way using java.time. I knew that GregorianCalendar could be used to handle Julian dates and that there is a JulianFields class in the java.time API. So I figured, there might be an easy way without having JulianChronology. But sure enough: Research enough and you don't have to ask anymore. – Paule Nov 08 '17 at 21:03
  • 2. If I had been sure that there is no way using java.time I might have asked that question. 3. If I had been sure that there is no way using java.time I might have searched explicitly for libraries. – Paule Nov 08 '17 at 21:06
  • But I tried quit a few searches like "convert .. from .. to in Java 8" without coming across the Threeten-Extra or Time4J projects. – Paule Nov 08 '17 at 21:14
  • @Paule Well, maybe I was a little bit too hard because the existence of class `JulianFields` is often mistaken by many people to believe in the support of Julian calendar but both things are quite different (Julian fields are not themselves a calendar but a common way of continuous counting in days for any calendar). Here the naming is indeed misleading. – Meno Hochschild Nov 09 '17 at 08:53