'How to convert a D M Y HH:MM:SS string in Java to Julian Date?

I have some strings in Java that come in the format: Day Month Year Hour:Minute:Second

7 Jan 2010 23:00:00.000
4 Feb 2010 17:40:00.000

What is the easiest way to parse this string and convert the values to their resulting Julian Dates? I am reading in these strings from Excel so they are not objects with any sort of conversion/formatting utilities (just raw strings). Is there an easy library or function to call to convert these, or would I have to manually write a parser? Thanks



Solution 1:[1]

My library Time4J supports Julian Dates out of the box.

    ChronoFormatter<PlainTimestamp> f =
        ChronoFormatter.ofTimestampPattern(
            "d MMM uuuu HH:mm:ss.SSS", PatternType.CLDR, Locale.ENGLISH);
    Moment j2000 = f.parse("7 Jan 2010 23:00:00.000").atUTC(); // are your timestamps really UTC?
    // eventually also: ".in(Timezone.ofSystem());"

    System.out.println(JulianDay.ofSimplifiedTime(j2000)); // programmer's standard
    // JD(POSIX)2455204.4583333335
    System.out.println(JulianDay.ofEphemerisTime(j2000)); // astronomical definition
    // JD(TT)2455204.459099352

Advantages:

  • No complex calculation of your owns.
  • Support for the astronomical definition on the time scale TT.
  • Explicit display of time zone dependencies (whatever you choose).

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Meno Hochschild