tl;dr
- Use java.time classes
- Specify your desired/expected time zone
Ex:
Duration.between(
Instant.EPOCH ,
Instant.now()
)
Avoid legacy date-time classes
The terrible old date-time classes bundled with the earliest versions of Java were supplanted years ago by the java.time classes.
For a moment seen through the wall-clock time used by the people of a particular region (a time zone), use ZonedDateTime
. For a moment in UTC, use Instant
.
Use proper time zone names
Specify a proper time zone name in the format of Continent/Region
, such as Europe/Paris
, Africa/Casablanca
, or Pacific/Auckland
. Never use the 2-4 letter abbreviation such as EST
or IST
as they are not true time zones, not standardized, and not even unique(!).
ZoneId z = ZoneId.of( "America/Montreal" ) ;
ZoneId
number os secs from DEC 31ST 1969 7 PM EST
Firstly, EST
is not a time zone, as discussed above. I assume you mean a time zone from the east coast of North America such as America/New_York
.
ZoneId z = ZoneId.of( "America/New_York" ) ;
ZonedDateTime
For a moment in a time zone, we need ZonedDateTime
.
ZonedDateTime zdt = ZonedDateTime.of( 1969 , 12 , 31 , 17 , 0 , 0 , 0 , z ) ;
zdt.toString(): 1969-12-31T17:00-05:00[America/New_York]
Duration
To represent a span of time as a count of seconds, use the Duration
class.
ZonedDateTime now = ZonedDateTime.now( z ) ;
Duration d = Duration.between( zdt , now ) ;
now.toString(): 2018-12-26T19:32:08.136846-05:00[America/New_York]
d.toString(): PT429410H32M8.136846S
Ask for the entire span-of-time in terms of whole seconds.
long wholeSeconds = d.getSeconds();
wholeSeconds: 1545877928
Instant
I noticed your origin time is of a few hours before 1970 in EST happens to be in UTC the first moment of 1970. That moment, 1970-01-01T00:00:00Z, is a common epoch reference, commonly known as Unix Time. That moment is also the epoch reference used by the java.time classes.
I suspect you fell into the trap of parochial thinking, using the perspective of your own personal time zone. This is not wise when programmer. When the programmer is on the job, you should be thinking in UTC, storing in UTC, logging in UTC, and exchanging data in UTC, nearly all the time. Only apply a time zone when required by business logic or when expected by the user in the user-interface.
To track a moment in UTC, use Instant
. For the epoch reference of 1970-01-01T00:00:00Z, use the constant Instant.EPOCH
.
Duration d = Duration.between( Instant.EPOCH , Instant.now() ) ;
About java.time
The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date
, Calendar
, & SimpleDateFormat
.
The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.
To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.
You may exchange java.time objects directly with your database. Use a JDBC driver compliant with JDBC 4.2 or later. No need for strings, no need for java.sql.*
classes.
Where to obtain the java.time classes?
The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval
, YearWeek
, YearQuarter
, and more.