There is a date in seconds since January 1, 1900: -699848845. How to calculate the normal date?
2 answers
Old API (up to Java 8):
Calendar cal = new GregorianCalendar(); cal.setTimeZone( TimeZone.getTimeZone( "UTC" ) ); // не указано, в какой зоне 1 января 1900, будет в UTC cal.clear(); cal.set( 1900, 0, 1 ); // январь - нулевой месяц SimpleDateFormat format = new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss.SSS zzzz" ); format.setTimeZone( TimeZone.getTimeZone( "UTC" ) ); System.out.println( format.format( cal.getTime() ) ); // 1900-01-01 00:00:00.000 Coordinated Universal Time cal.add( Calendar.SECOND, -699_848_845 ); System.out.println( format.format( cal.getTime() ) ); // 1877-10-27 21:32:35.000 Coordinated Universal Time New API (Java 8 java.time ):
LocalDateTime result = LocalDateTime.of( 1900, 1, 1, 0, 0 ) // январь - 1 месяц .plusSeconds( -699_848_845 ); System.out.println( result ); // 1877-10-27T21:32:35 The result is displayed in the modern calendar (ISO-8601), excluding the time zone. For the dense past, you may have to connect ThreeTen-extra , where there is a realization of the Julian calendar, and understand the subject in more detail.
- Cool! Thank you) I'll save to the piggy bank. - exStas
|
Add the joda-time dependencies and get the result:
public class TestDate { public static final DateTimeFormatter FULL_FORMATTER = DateTimeFormat.forPattern("yyyy-MM-dd").withLocale(Locale.getDefault()); public static void main(String[] args) { DateTime dt = DateTime.parse("1900-01-01", FULL_FORMATTER); int seconds = 699848845; System.out.println(FULL_FORMATTER.print(dt.minusSeconds(seconds))); } } |
1877-10-27 21:32:35- splash58