Convert a Julian Date to an Instant

前端 未结 4 1516
再見小時候
再見小時候 2021-01-05 14:58

I\'m running into a situation where I would like to convert from a Julian date to an java.time.Instant (if that makes sense), or some Java time that can be more easily under

4条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-01-05 15:11

    The most complete and also shortest approach is obtained by my library Time4J, see this snippet using the class JulianDay:

    double customJD = 95906.27600694445; 
    
    // my comment: I have never seen Julian days with that epoch until now    
    HistoricCalendar hcal = // date when the new style calendar act took effect
        HistoricCalendar.of(ChronoHistory.of(Locale.UK), HistoricEra.AD, 1752, 9, 14);
    
    // last term 0.5 necessary because julian days start at noon
    double value = customJD + hcal.get(EpochDays.JULIAN_DAY_NUMBER) - 0.5;
    JulianDay jd = JulianDay.ofSimplifiedTime(value);
    Instant instant = jd.toMoment().toTemporalAccessor();
    
    System.out.println(instant); // 2015-04-15T06:37:27Z
    

    However, it should be noted that the most dominating field of application of Julian days is the astronomy, see also the official recommendation of the IAU. And on that field, it is much more common to include a delta-T-correction i.e. to define the Julian days on the time scale TT (Terrestrial Time). Time4J offers the methods JulianDay.ofEphemerisTime(...) for this purpose. If you seriously consider to handle time scales such as TT then you should rather work with the class Moment instead of Instant because last one cannot understand TT, UTC, UT including leap second handling etc.

提交回复
热议问题