简体   繁体   中英

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

I have date time values coming from sas ( 10 digits) that look like 1627741415 . I want to convert them in my java code to generate Java date time YYYY-MM-dd HH:mm:ss.0

I cant find how the SAS date time works for this.
1627741415 corresponds to July 31, 2011 2:33:35 pm and so I want it to be

2011-07-31 14:33:35.0.

Any help is appreciated. Thanks in advance

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

I did a quick test using 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 could use the difference of 315618600 to calculate your dates.

so say you have 1627741415 you subtract 315618600 to endup with an epoch date that you can use normally in your application.

This SAS doc page explains the situation.

Epoch

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

Note that all three of the common Java date-time frameworks:

…use a different epoch. All three use the Unix epoch , first moment of 1970 (not 1960) in UTC.

Granularity

Furthermore, granularity is different. The java.time package resolves to nanoseconds , while the other two resolve to milliseconds . In contrast, SAS resolves to whole seconds.

JDBC

While I did not study the details, that doc seems to claim that their JDBC driver for SAS should handle the SAS-Java conversions for you. So, no need for you to handle the integer number of seconds from epoch. Let the driver do the work.

By the way, note that in Java 8 and later, the java.sql.Timestamp and related classes have been enhanced with convenience methods ( toInstant , etc.) for converting to and from the new java.time data types. This is a stop-gap measure until JDBC drivers are eventually updated to directly handle the new types.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM