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);
System.out.println(startTime.toString());
// Conversion of data type: AbsoluteDate to Date
Date epoch = startTime.toDate(UTC);
System.out.println(epoch.toString());
Output is:
2021-10-05T10:15:14.107968Z
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.
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.
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?
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
System.out.println(fmt.format(epoch));
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.