How to check whether a given date is between two dates but it should not check with the time in given date.
I have tried with after but it checks for time range too.
Could anyone help me to know about this ?
TIA.,
How to check whether a given date is between two dates but it should not check with the time in given date.
I have tried with after but it checks for time range too.
Could anyone help me to know about this ?
TIA.,
The old date-time classes bundled with earlier versions of Java have been supplanted with the java.time framework built into Java 8 and later.
The LocalDate
class represents a date-only value, without time-of-day and without time zone.
LocalDate start = LocalDate.of( 2016 , 1 , 1 ) ;
LocalDate stop = LocalDate.of( 2016 , 1 , 23 ) ;
To get the current date, specify a time zone. For any given moment, today’s date varies by time zone. For example, a new day dawns earlier in Paris than in Montréal.
LocalDate today = LocalDate.now( ZoneId.of( "America/Montreal" ) );
We can use the isEqual
, isBefore
, and isAfter
methods to compare. In date-time work we commonly use the Half-Open approach where the beginning of a span of time is inclusive while the ending is exclusive.
Boolean containsToday = ( ! today.isBefore( start ) ) && ( today.isBefore( stop ) ) ;
The java.time framework can be extended by adding the ThreeTen-Extra project’s jar to your project. In particular you may find the Interval
class handy.
You can use the DateUtils.truncate from Apache Commons library.
Example:
DateUtils.truncate(new Date(), java.util.Calendar.DAY_OF_MONTH)
Another option could be to use JODA-TIME which opens you the API for Local Date
Example:
DateTime first = ...;
DateTime second = ...;
LocalDate firstDate = first.toLocalDate();
LocalDate secondDate = second.toLocalDate();
Then you could just compare both with JODA-TIME compareTo( ) or after () from the library :)