Question

How can I calculate the Modified Julian day from a JSR-310 class such as LocalDate? (in JDK 8)

Specifically, this is the calculation of the continuous count of days known as "Modified Julian day", not the date in the Julian calendar system.

For example:

LocalDate date = LocalDate.now();
long modifiedJulianDay = ???
Was it helpful?

Solution

Short answer:

LocalDate date = LocalDate.now();
long modifiedJulianDay = date.getLong(JulianFields.MODIFIED_JULIAN_DAY);

Explanation:

The Wikipedia article gives the best description of Julian day as a concept. Put simply, it is a simple, continuous, count of days from some epoch, where the chosen epoch gives the variation its name. Thus, Modified Julian Day counts from 1858-11-17.

JSR-310 date and time objects implement the TemporalAccessor interface which defines the methods get(TemporalField) and getLong(TemporalField). These allow the date/time object to be queried for a specific field of time. Four field implementations are provided offering Julian day variations:

These fields can only be used with getLong(TemporalField) because they return a number that is too large for an int. If you call now.get(JulianFields.MODIFIED_JULIAN_DAY) then an exception will be thrown: "UnsupportedTemporalTypeException: Invalid field ModifiedJulianDay for get() method, use getLong() instead"

Note that JSR-310 can only provide integral numbers from TemporalField, thus the time-of-day cannot be represented, and the numbers are all based on midnight. The calculations also use local midnight, not UTC, which should be taken into account.

The fields can also be used to update a date/time object using a method on Temporal:

result = input.with(JulianFields.MODIFIED_JULIAN_DAY, 56685);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top