tl;dr
myPreparedStatement.setObject( // Generally best to use Prepared Statement, to avoid SQL Injection risk.
1 , // Specify which placeholder `?` in your SQL statement.
LocalDate.now( ZoneId.of( "Pacific/Auckland" ) ) // Determine today’s date as seen in the wall-clock time used by the people of a certain continent/region (a time zone).
.atStartOfDay( ZoneId.of( "Pacific/Auckland" ) ) // Determine the first moment of the day on that date in that zone. Not always 00:00:00.
.toInstant() // Adjust that first moment of the day in that zone to the wall-clock-time of UTC.
.getEpochSecond() // Get a count of whole seconds since the epoch reference of 1970-01-01T00:00:00Z.
)
java.time
The modern approach uses the java.time classes that supplanted the troublesome old legacy date-time classes.
In this, due_date column is INTEGER, i just want the records which matches today's date(current date) alone in due_date without considering time.
LocalDate
The LocalDate
class represents a date-only value without time-of-day and without time zone.
A time zone is crucial in determining a date. For any given moment, the date varies around the globe by zone. For example, a few minutes after midnight in Paris France is a new day while still “yesterday” in Montréal Québec.
If no time zone is specified, the JVM implicitly applies its current default time zone. That default may change at any moment, so your results may vary. Better to specify your desired/expected time zone explicitly as an argument.
Specify a proper time zone name in the format of continent/region
, such as America/Montreal
, Africa/Casablanca
, or Pacific/Auckland
. Never use the 3-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" ) ;
LocalDate today = LocalDate.now( z ) ;
If you want to use the JVM’s current default time zone, ask for it and pass as an argument. If omitted, the JVM’s current default is applied implicitly. Better to be explicit, as the default may be changed at any moment during runtime by any code in any thread of any app within the JVM.
ZoneId z = ZoneId.systemDefault() ; // Get JVM’s current default time zone.
Epoch seconds
We need to translate that date to a number, the count of seconds since the epoch reference of first moment of 1970 in UTC.
To do that, we need to get the first moment of the day. Let java.time determine that first moment. Do not assume the day starts at 00:00:00. Anomalies such as Daylight Saving Time (DST) mean the day may start at another time such as 01:00:00. Specify a time zone to get a ZonedDateTime
object.
ZonedDateTime zdt = today.atStartOfDay( z ) ;
We must adjust into UTC from our zone. Easiest way to do this is to extract an Instant
. The Instant
class is always in UTC by default.
Instant instant = zdt.toInstant() ; // Adjust from our zone to UTC.
Get the number of seconds since epoch.
long secondsSinceEpoch = instant.getEpochSecond() ;
As a shortcut, we could have asked for seconds-from-epoch directly from the ZonedDateTime
class. But I wanted to make clear that we are working adjusting from a particular time zone to UTC.
long startSecondsSinceEpoch = zdt.toEpochSecond() ; // Shortcut for adjusting from time zone to UTC, and then getting count of seconds since epoch.
We also need the ending of our time range, to find all records whose date is today’s date. We will use the first moment of the following day. This follows the usual practice of Half-Open definition of a span of time. In Half-Open, the beginning is inclusive while the ending is exclusive.
long stopSecondsSinceEpoch = ld.plusDays( 1 ).atStartOfDay( z ).toEpochSecond() ;
SQL
Now we are ready for the SQL statement. Generally best to make a habit of using a prepared statement.
String sql = "SELECT when FROM tbl WHERE when >= ? AND when < ? ; " ;
…
myPreparedStatement.setObject( 1 , startSecondsSinceEpoch ) ;
myPreparedStatement.setObject( 2 , stopSecondsSinceEpoch ) ;
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.