How to translate Date and time from Date to seconds from the Birth of Christ in Java?


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

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

For example, 2018-12-26 11: 39: 27 should be converted to seconds from 01.01.0001 00: 00: 00

Correction: It would be more correct to use Timestamp instead of Date There is a service that needs to transmit the date and time in seconds. This service uses the following method to play back from seconds to 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);
        } 

Accordingly, the date and the time (for example, 2018-12-26 11: 39: 27) must first be converted to seconds, with the beginning of the calculation from 01.01.0001 00: 00: 00

1 answers

This code 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 the clarification in the question:

In fact, the code you provided does not take into account time zones, that is, it converts the number of seconds that have elapsed since 0001.01.01 00:00 local time (no matter what) to a date in the same local time. If this is called the number of seconds "from the birth of Christ", then this code can be recognized as correct only in the time zone in which Jesus is located was born. Here is the code that gets Timestamp (if you really want to, although I don't understand why you need Date or "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); }
}
 4
Author: m. vokhm, 2019-02-07 14:23:41