How to translate the date and time from Date to seconds from the birth of Christ?

date.getTime() - returns from the beginning of UNIX time.

It is necessary, for example, 2018-12-26 11:39:27 to be translated into seconds as of January 1, 0001 00:00:00

Specification: It is more correct to use not Date, but Timestamp. There is a service to which you need to transfer the date and time in seconds. This service uses the following method to reproduce from seconds back to the date and time:

  private String dateConverter(String date) { LocalDateTime ldt = LocalDateTime.of(1, Month.JANUARY, 1, 0, 0, 0); LocalDateTime resDate = ldt.plusSeconds(Long.parseLong(date)); return String.valueOf(resDate); } 

Respectively, the date and time (for example, 2018-12-26 11:39:27) must first be translated into seconds, starting from 01.01.0001 00:00:00

  • four
    I would probably ask 5 times from the developers of the service that this is the case. suddenly they called it unixtime. Unless of course this is not some kind of historical service, which you need to pass the time before the year 70 - Mike
  • 2
    @AntonSorokin Dates 00.00.0000 does not exist :) There is no zero year (like month and day). After 1 BC is 1 AD - Enikeyschik February
  • 3
    @ LeonidDubravsky In any case, check with the developers of the service how many seconds they had before 1970. because there may be different opinions and methods of calculating this value - Mike
  • 2
    Are you aware that Jesus was born later than the “Nativity of Christ”?) So please specify the specific data in the question - Kir_Antipov February
  • 2
    And the time before 1970, no one bothers to transmit a negative number. - Enikeyschik February

1 answer 1

Here is the code that returns what you need:

  ZonedDateTime localNow = Instant.now().atZone(ZoneOffset.systemDefault()); ZonedDateTime localAtCristsBirth = Instant.parse("0001-01-01T00:00:00.00Z").atZone(ZoneOffset.systemDefault()); Duration timePassed = Duration.between(localAtCristsBirth, localNow); System.out.format("От тождества Христова прошло %d секунд\n", timePassed.getSeconds() ); 

Clarification in response to clarification in the question:

In fact, the code you 0001.01.01 00:00 does not take into account time zones, that is, it converts the number of seconds elapsed from the time 0001.01.01 00:00 local time (no matter which one) to the date of the same local time. If this is called the number of seconds "from the birth of Christ", then this code can be considered correct only in the time zone in which Jesus was born. Here is the code that receives the Timestamp (since you really want to, although I don’t understand why you need a Date or a "more correct" Timestamp ) and returns exactly what you need (number of seconds from 00:00 01.01.01 local time as a string):

 package stackoverflow; import java.sql.Timestamp; import java.time.Duration; import java.time.LocalDateTime; import java.time.Month; import java.time.ZoneId; public class Ru_so_941590 { public static void main(String[] args) { Timestamp now = Timestamp.valueOf(LocalDateTime.now()); say("Местное время: " + now); String secondsAD_str = secsWouldHavePassedSinceChtistsBirthIfHeHadBeenBornHere(now); System.out.format("Если бы Спаситель родился здесь, " + "то с момента его рождения прошло бы\n\t\t %,d секунд\n", Long.parseLong(secondsAD_str) ); say("Converted back: " + dateStrFromSecondsAD(secondsAD_str)); } private static String secsWouldHavePassedSinceChtistsBirthIfHeHadBeenBornHere(Timestamp now) { LocalDateTime nowLocal = LocalDateTime.ofInstant(now.toInstant(), ZoneId.systemDefault()); LocalDateTime christsBirthday = LocalDateTime.parse("0001-01-01T00:00:00"); return String.valueOf(Duration.between(christsBirthday, nowLocal).getSeconds()); } private static String dateStrFromSecondsAD(String secondStr) { LocalDateTime christsBirth = LocalDateTime.of(1, Month.JANUARY, 1, 0, 0, 0); LocalDateTime resDate = christsBirth.plusSeconds(Long.parseLong(secondStr)); return String.valueOf(resDate); } static void say(String format, Object... args) { System.out.println(String.format(format, args)); } static void say(String s) { System.out.println(s); } } 
  • 3
    In different time zones, the same result will be? - Enikeyschik February
  • 3
    @ Enikeysh Yes, of course. If strictly from the birth of Christ, we must find out in which time zone he was born :) - m. vokhm pm
  • 2
    In UTC + 2 , of course :) - Enikeyschik February
  • @ Enikeyshik Well, actually, of course, it’s more logical to assume that he was born in Greenwich time :) Then for the second ZonedDateTime you need to specify UTC. - m. vokhm pm
  • Instead, Date used Timestamp, so it is more correct. ZonedDateTime localNow = timestampDate.toInstant (). AtZone (ZoneId.systemDefault ()); Further, all of your description. But it turns out the difference in 2 hours with the reverse conversion method from the description. - Leonid Dubravsky