Question

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.

Was it helpful?

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.

OTHER TIPS

The method posted here has essentially solved my needs: http://www.rgagnon.com/javadetails/java-0506.html#comment-811614512 You have to take in consideration that it returns the time at noon (12:00 UTC), which is one day later than the date at midnight (0:00 UTC).

To convert from milliseconds to (year, month, day) - which is the format the aforementioned method expects -, you may use these instruccions:

  Date date = new Date(longMillis);

  Calendar calendar = Calendar.getInstance();
  calendar.setTime(date);

  int year = calendar.get(Calendar.YEAR);
  int month = calendar.get(Calendar.MONTH) + 1; // El mes que devuelve Calendar va de 0 a 11.
  int day = calendar.get(Calendar.DAY_OF_MONTH);
  int[] ymd = new int[] {year, month, day};
    GregorianCalendar cal = new GregorianCalendar();
    cal.setTimeInMillis(millis);        
    cal.setGregorianChange(new Date(Long.MAX_VALUE));
    int julianDay = cal.get(Calendar.DAY_OF_YEAR);

tl;dr

Instant.ofEpochMilli( millisSinceJavaEpoch )                   // Moment in UTC.
       .atOffset( ZoneOffset.UTC )                             // `OffsetDateTime` object.
       .getLong( java.time.temporal.JulianFields.JULIAN_DAY )  // `long` such as 2457811.

java.time

Java 8 and later comes with the java.time classes, supplanting the troublesome old legacy date-time classes. See Oracle Tutorial.

"elapsed milliseconds since the beginning of the Unix Epoch"

If you mean the epoch of first moment of 1970 in UTC (1970-01-01T00:00:00Z), then we can convert your count-of-milliseconds into a Instant object. The Instant class represents a moment on the timeline in UTC with a resolution of nanoseconds (up to nine (9) digits of a decimal fraction).

long millisSinceJavaEpoch = Instant.now().toEpochMilli() ;  // Simulating your given number of milliseconds since 1970-01-01T00:00:00Z.

1488099298325

Instant instant = Instant.ofEpochMilli( millisSinceJavaEpoch );  // Convert from number to `Instant` object.

2017-02-26T08:54:58.325Z

We need a date from that Instant. I'm no expert on Julian chronology, but it seems that by convention people use UTC in mapping to the modern ISO 8601 chronology. So let's convert that Instant to a OffsetDateTime with the offset constant ZoneOffset.UTC.

OffsetDateTime odt = instant.atOffset( ZoneOffset.UTC );  // Convert from `Instant` to `OffsetDateTime` with UTC as assigned offset.

2017-02-26T08:54:58.325Z

From the OffsetDateTime we can access a TemporalField. As our implementation of TemporalField we want JULIAN_DAY constant from the java.time.temporal.JulianFields class.

long jd = odt.getLong( java.time.temporal.JulianFields.JULIAN_DAY );  // Convert to Julian Day (number of whole days since January 1, 4713 BCE in the Julian calendar, a.k.a. -4713-11-24 Gregorian).

2457811

This result matches the result given at the web site you mentioned.

See this code above run live at IdeOne.com.

Depending on your definition, you may want MODIFIED_JULIAN_DAY instead of JULIAN_DAY.


About java.time

The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date, Calendar, & SimpleDateFormat.

The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.

To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.

Where to obtain the java.time classes?

The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval, YearWeek, YearQuarter, andfz more.


org.threeten.extra.chrono.JulianChronology

Those java.time classes are extended by the ThreeTen-Extra project. That project includes a proleptic implementation of the Julian calendar system, JulianChronology.

    Time time = new Time();
    time.setToNow();
    long currentTime = Long.parseLong("1484903678788");
    int currentJulianDay = Time.getJulianDay(currentTime, time.gmtoff);
    System.out.print(currentJulianDay); // 2457774
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top