Question:
How to convert date and time from Date
to seconds from the birth of Christ?
date.getTime()
– returns from the beginning of UNIX time.
For example, you need to translate 2018-12-26 11:39:27 into seconds from 01/01/0001 00:00:00
Clarification: It would be more correct to use not Date, but Timestamp There is a service that needs to transfer the date and time in seconds. This service reproduces from seconds back to date and time using the following method:
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 converted to seconds, with the beginning of the calculation from 01/01/0001 00:00:00
Answer:
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 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 passed since 0001.01.01 00:00
local time (no matter which one) into a date at the same local time. If this is called the number of seconds "from the birth of Christ", then this code can only be recognized as correct in the time zone in which Jesus was born. Here is the code that receives a Timestamp
(since you really want to, although I don't understand why you need a Date
or a "more correct" Timestamp
here) and returns exactly what you need (number of seconds since 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); }
}