I want to convert julian seconds to date in java

107 Views Asked by At

I am getting data like this from a device 1348398464 and its date time is like time: 11:07:44

date: 28:09:2022

I don't know how it's decoded

1

There are 1 best solutions below

2
rzwitserloot On

Julian? As in The Julian Calendar that most of the world stopped using around 1582 (using the gregorian calendar instead which is a very minor tweak), Russia stopped using in 1918 (explaining why the October Revolution was actually in November, but in Russia, still on Julian, it was october for them), and which is only still used for orthodox church procedures (such as picking religious dates), and in parts of Morocco?

That seems.. unlikely.

The java.time package no longer includes support for Julian out of the box. The library that java.time is based on (JSR-310), does. The extra parts are still available as ThreeTen-extra.

Include it and you can do this.

In basis it seems like 1348398464 represents the amount of seconds that have passed since midnight, 1980, at UTC, but using the julian calendar.

Using the gregorian calendar instead:

int x = 1348398464;
Instant epoch = ZonedDateTime.of(LocalDate.of(1980, 1, 1), 
  LocalTime.MIDNIGHT, ZoneOffset.UTC).toInstant();
Instant mark = epoch.plusSeconds(x);
System.out.println(mark);

1980 is a weird epoch (the computer using world pretty much always uses 1970 instead).

Gets you 2022-09-23T11:07:44Z which is close to what you wanted. The difference of 5 days is probably indeed explained by the fact you're on the julian calendar (that, or the epoch is midnight, january 5th, 1980 - which is bizarre).

Run the same code but with the julian chronology from threeten-extra:

Instant epoch = ZonedDateTime.of(LocalDate.of(1980, 1, 1), 
  LocalTime.MIDNIGHT, ZoneOffset.UTC).toInstant();

ChronoZonedDateTime<JulianDate> mark = JulianChronology.INSTANCE.zonedDateTime(epoch, ZoneOffset.UTC);

int x = 1348398464;
mark = mark.plusSeconds(x);
System.out.println(mark);

You may have to play around with the epoch value; possibly it's 1980-1-1 in julian and not in gregorian as the above code does (start with Instant epoch = Instant.ofEpochMilli(0) instead, then make a julian out of date, then call .addYears(10), then .addSeconds(1348398464), see if it works out that way).