2

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.

Josep
  • 495
  • 2
  • 6
  • 16
  • Have you tried with [Time](http://docs.oracle.com/javase/7/docs/api/java/sql/Time.html) class? – Quirin Feb 25 '13 at 12:33
  • You would need to use [JodaTime](http://joda-time.sourceforge.net/index.html) - there is no implementation for a `JulianCalendar` in the JDK. – Boris the Spider Feb 25 '13 at 12:33
  • Is there any sample code showing how to import and use the functionality? – Josep Feb 25 '13 at 12:39
  • @Josep - there is a code sample on the [Joda Time Julian Calendar page](http://joda-time.sourceforge.net/cal_julian.html). Include the joda jar's by downloading them, or pulling them into your project with Maven. – Perception Feb 25 '13 at 12:41
  • **Java 8 and later** does indeed have some limited built-in support for this Julian calendar. See [my Answer](http://stackoverflow.com/a/42081407/642706) below. – Basil Bourque Feb 26 '17 at 01:54

5 Answers5

2

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.

igr
  • 10,199
  • 13
  • 65
  • 111
  • Looks good. What's the name of the package? Where can I download it? – Josep Feb 25 '13 at 13:01
  • The full class name is **jodd.datetime.JDateTime**. It is part of **jodd-core** module. You have various options for download: http://jodd.org/download/index.html – igr Feb 25 '13 at 13:03
  • Sorry. My analyst and me had some trouble accessing the repository, so I eventually chose another option. – Josep Feb 26 '13 at 08:40
  • The repository is on GitHub, that works quite good. If it doesn't work, you have the Maven central repo, where you can download both binary and source jar; even if you don't use Maven. And finally, you can download binaries directly from the website. Not to mention that for any reason you can contact support. I do not understand your decision. – igr Feb 26 '13 at 09:51
  • It was probably my fault; I don't deny that. And your commitment to help surely deserves at least an up vote, so here you are. I just feel it's somehow dishonest, by my part, to accept your answer if I was unable to use it. – Josep Feb 26 '13 at 10:18
  • It's not about the votes in [SO] - it's about the open-source:) I still don't understand you saying 'I was/am unable to use' with so many options and READMEs available (github and maven are so common places today for sharing source); not clarifying what is exactly the problem with the repo. Anyhow, from my view, it make sense to *grow* the existing, maintained and tested open source (that seems to be a solution for you); then to take some simple online example. If you like, i can send you all sources on your personal email; just contact me. – igr Feb 26 '13 at 10:49
  • Take in consideration that "milliseconds" must be a "long" variable, not a "Long" object. I tried to edit the answer to reflect this fact, but the edit was rejected with 3 votes against 1. – Josep Mar 11 '13 at 14:12
1

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};
Josep
  • 495
  • 2
  • 6
  • 16
  • This code just uses the date (year, month, day) and does not count the time component (hour, minute, second, millisecond). – igr Feb 26 '13 at 09:57
  • Again, your observation is right. But I felt that converting to (year, month, day) was much easier than converting to Julian Day Number. That's why I found the aforementioned method very useful. – Josep Feb 26 '13 at 10:21
0
    GregorianCalendar cal = new GregorianCalendar();
    cal.setTimeInMillis(millis);        
    cal.setGregorianChange(new Date(Long.MAX_VALUE));
    int julianDay = cal.get(Calendar.DAY_OF_YEAR);
NickJ
  • 9,380
  • 9
  • 51
  • 74
  • 2
    No. That's not what I'm looking for. I need the astronomical Julian day number. Sorry. – Josep Feb 25 '13 at 12:51
0
    Time time = new Time();
    time.setToNow();
    long currentTime = Long.parseLong("1484903678788");
    int currentJulianDay = Time.getJulianDay(currentTime, time.gmtoff);
    System.out.print(currentJulianDay); // 2457774
Duong.Nguyen
  • 373
  • 3
  • 13
  • Are you missing an `import` statement? Without that, it's unclear which `Time` you are using! It's also not obvious why you're using the `gmtoff` for "now" with a `currentTime` that's not "now". – Toby Speight May 05 '17 at 13:21
-1

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.

Community
  • 1
  • 1
Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154