java.time
Do not perform business logic on java.sql.Timestamp
objects. That class is part of the legacy date-time classes bundled with the earliest versions of Java that have proven to be poorly designed, confusing, and troublesome. Avoid these classes where possible.
The java.time framework built into Java 8 and later supplants these old classes.
Much of the java.time functionality has been back-ported to Java 6 & 7 and further adapted for Android.
Instant
A java.time.Instant
object represents a moment on the timeline in UTC with a resolution of nanoseconds.
If using a JDBC 4.2 driver, you may be able to call getObject
to retrieve a SQL column of TIMESTAMP WITH TIME ZONE type directly into a Instant
object.
If not able to get an Instant
directly, get a java.sql.Timestamp
and immediately convert to java.time. New methods added to the old classes facilitate conversions.
Instant instant = mySqlTimestamp.toInstant();
Duration
With a pair of Instant
objects, define the elapsed span of time between them as a Duration
.
Duration duration = Duration.between( start , stop );
Ask for the total number of hours in the entire span of time.
long totalHours = duration.toHours();
See this similar Question for more discussion.