Calculate Modified Julian Day in JSR-310

你说的曾经没有我的故事 提交于 2019-12-07 04:46:55

问题


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 = ???

回答1:


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 Day
  • JulianFields.JULIAN_DAY - a midnight-based variation of the standard Julian day concept
  • JulianFields.RATA_DIE - a Julian day variation based on the Gregorian common era
  • ChronoField.EPOCH_DAY - a Julian day variation based on the standard Java/UNIX 1970-01-01

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


来源:https://stackoverflow.com/questions/21404159/calculate-modified-julian-day-in-jsr-310

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