Search code examples
javajulian-date

How can I convert milliseconds to Julian Day Number in Java?


I need a Java method that converts from "elapsed milliseconds since the beginning of the Unix Epoch" to "Elapsed days since January 1, 4713 BC". Is this functionality already implemented is Java?

I have tried several options. Some of them don't compile. Some of them are incoherent with the conversions available on line (http://www.onlineconversion.com/julian_date.htm). So, please, post only answers that you have satisfactorily used yourselves.


Solution

  • Try JDateTime from Jodd. This is date-time class that performs all its calculations over Julian Date Numbers. JdateTime class has friendly user interface and any time you can get the value of Julian Date. For example, you can instantiate the class with epox milliseconds and then read the Julian Date Number:

    JDateTime jdt = new JDateTime(milliseconds);
    JulianDateStamp jds = jdt.getJulianDate();
    // or:
    double jdn = jdt.getJulianDateDouble()
    // or:
    int jdn = jdt.getJulianDateNumber()
    

    If you are interested in concrete calculation, see method TimeUtil#toJulianDate. It is based on proven astronomical algorithm, but it also takes care about calculation errors of floating-points. Thats why the result is JulianDateStamp, class that separately holds JD integer and its fraction, to provide precision up to 1 millisecond. This class also has many test cases written.