Calculate elapsed time until deadline
With JDBC 4.2 and later, retrieve the date-time using the modern java.time classes.
OffsetDateTime odt = myResultSet.getObject( … , OffsetDateTime.class ); // Likely to be a UTC value.
Get current moment.
OffsetDateTime now = OffsetDateTime.now( ZoneOffset.UTC ) ;
Calculate elapsed time.
Duration d = Duration.between( now , odt ) ;
Schedule event
Use the modern Executor framework rather than the legacy Timer
class. This is especially true in a Servlet or Jakarta.ee (Java EE) environment.
Define your executor, specifically a ScheduledExecutorService
. We need only a single thread for this task.
ScheduledExecutorService scheduledExecutorService = Executors.newSingleThreadScheduledExecutor()
Ask that executor to wait a certain amount of time, then run your code to check the status in the database.
myExecutorService.schedule( // Tell the executor what you want to run, and when you want it run on your behalf in a background thread.
new Runnable() { … } , // Define task to be executed as a `Runnable`.
d.toSeconds() , // Amount of time to wait until execution. Use self-documenting code rather than a “magic number” such as `86400000`.
TimeUnit.SECONDS // Specify the granularity of time used in previous pair of arguments.
) // Returns a `ScheduledFuture` which you may want to cache.
Optionally, you can capture the ScheduledFuture
returned by this schedule
statement. You can then monitor completion status.
In your Runnable
, retrieve the current status record from your database. If warranted, schedule another execution, re-calculating time to wait using a new current moment.
Servlet web container
For a Servlet web container such as Tomcat or Jetty without the full Jakarta.ee stack, the above discussion applies.
Furthermore, you will want to write a class that implements the ServletContextListener
interface where you can set-up and tear-down your executor service. You need to gracefully tear-down the executor service so that its background thread does not continue merrily on its way long after your web app has shutdown.
Jakarta EE
If in a full-blown Jakarta.ee stack such as Glassfish, this work is much easier.
The executor service seen above can be automated, with set-up and tear-down performed on your behalf. Use the concurrency utilities defined in JSR 236: Concurrency Utilities for JavaTM EE. This has been covered many times, so search Stack Overflow for more info.
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.