Calculate Modified Julian Day in JSR-310

不问归期 提交于 2019-12-05 06:38:09

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);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!