Convert SAS date value to Java YYYY-MM-DD HH: mm: ss

I have time-to-time values ​​coming from sas (10 digits) that looks like 1627741415

. I want to convert them to my Java code to generate Java date YYYY-MM-dd HH: mm: ss. 0

I cannot find how the SAS date works for this.
1627741415

matches July 31, 2011 2:33:35 p.m.

, so I want it to be

2011-07-31 14:33:35.0.

      

Any help is appreciated. thanks in advance

+3


source to share


2 answers


Because SAS datetime is a value that represents the number of seconds between January 1st, 1960 and the specified date. I think you could use the difference from the era, which is from 1970.

I quickly checked the code:

 LocalDateTime dateTime = LocalDateTime.parse("2011-07-31T14:33:35.0");
 System.out.println(dateTime.toString());
 System.out.println(dateTime.toEpochSecond(ZoneOffset.UTC));
 System.out.println(" Difference : "+ (1627741415 - dateTime.toEpochSecond(ZoneOffset.UTC) ));

      



And you can use the difference 315618600 to calculate your dates.

So, say you have 1627741415 you subtract 315618600 in order to arrive at an epoch date that you can normally use in your application.

+3


source


This page of the SAS document explains the situation.

Epoch

SAS uses the epoch of the first moment of 1960, presumably in UTC. SAS DATE is the number of whole days since then. SAS DATETIME is the number of whole seconds.

Note that all three common Java date frameworks are:

... use a different era. All three use Unix epoch , the first time 1970 (not 1960) in UTC.



Grain

Also, the granularity is different. The java.time package resolves nanoseconds , and the other two resolve milliseconds . In contrast, SAS is allowed in whole seconds.

JDBC

Until I went into the details, this document seems to state that their JDBC to SAS should handle SAS-Java conversions for you. This way, you don't need to handle the whole number of seconds from the epoch. Let the driver do the job.

By the way, note that in Java 8 and later, java.sql.Timestamp and related classes have been extended with convenience methods ( toInstant

and so on) to convert to and from the new java.time datatypes. This is a stopping measure until the JDBC drivers are eventually updated to handle the new types directly.

+2


source







All Articles