Conversion of AbsoluteDate to Java Date

Hello Orekit community,

First time using Orekit for a project, and I was wondering if I can get a better understanding for the following issue.

I wanted to convert the Orekit AbsoluteDate object to a Java Date object while keeping the date/time representation exactly the same. I am using the following code:

final TimeScale UTC = TimeScalesFactory.getUTC();
AbsoluteDate startTime = new AbsoluteDate(“2021-10-05T10:15:14.107968Z”, UTC);

// Conversion of data type: AbsoluteDate to Date
Date epoch = startTime.toDate(UTC);

Output is:

Tue Oct 05 06:15:14 EDT 2021

My question is, why is there a time conversion change happening? If you could help understand this, I’d greatly appreciate it.

Hey there,

Your second output is in the EDT timezone
I suspect the string/print method uses local information from your computer. So I would say it’s a pure display issue, your date is correctly defined.


Hi Serrof,

Thank you for your input. When converting to Java Date object from AbsoluteDate using the .toDate() method, is there such a way to output the date such that it results in following output?

5 Oct 2021 10:15:14.107967

Hi @alexf ,

You can use DateFormat to output the specified string.

        // Conversion of data type: AbsoluteDate to Date
        Date epoch = startTime.toDate(utc);
        DateFormat fmt = new SimpleDateFormat("d MMM yyyy HH:mm:ss.SSS", Locale.US);
        fmt.setTimeZone(TimeZone.getTimeZone("GMT"));  // set timezone to GMT

It will output “5 Oct 2021 10:15:14.108”.

1 Like

Thanks! This is what I needed!

Keep in mind this won’t work during a leap second as Date doesn’t support them.

Do you have any suggestions to ensure this is taken into account? Thanks in advance!

AbsoluteDate.toStringRfc3339 or AbsoluteDate.tostringWithoutUtcOffset, or similar methods in DateTimeComponents will give correct results. Formatting a date gets to be a bit tricky because the year is unknown until the seconds value is rounded. (assuming you want rounding instead of truncation.) Orekit doesn’t have general purpose date formatting code, but perhaps that should be added work correctly with rounding.

1 Like