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 = ???
A modified version of the Julian date denoted MJD obtained by subtracting 2,400,000.5 days from the Julian date JD, The MJD therefore gives the number of days since midnight on November 17, 1858. This date corresponds to 2400000.5 days after day 0 of the Julian calendar.
The expression y/4 - y/100 + y/400 (all integer divisions) calculates the number of leap years since the year –4800 (which corresponds to a value of 0 for y). Recall that there is a leap year every year that is divisible by 4, except for years that are divisible by 100, but not divisible by 400.
In this example, we are starting with the representation YYYYDDD. YYYY represents the four digits of the year. DDDD represents the day of the year. To get the Julian Days by itself in a numeric format, divide the date by 1000 and ignoring the decimal and then multiplying it back by 1000.
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:
JulianFields.MODIFIED_JULIAN_DAY
- the standard Modified Julian DayJulianFields.JULIAN_DAY
- a midnight-based variation of the standard Julian day conceptJulianFields.RATA_DIE
- a Julian day variation based on the Gregorian common eraChronoField.EPOCH_DAY
- a Julian day variation based on the standard Java/UNIX 1970-01-01These 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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With