Check Date With Todays Date
Solution 1:
Don't complicate it that much. Use this easy way. Import DateUtils java class and call the following methods which returns a boolean.
DateUtils.isSameDay(date1,date2);
DateUtils.isSameDay(calender1,calender2);
DateUtils.isToday(date1);
For more info refer this article DateUtils Java
Solution 2:
Does this help?
Calendar c = Calendar.getInstance();
// set the calendar to start of today
c.set(Calendar.HOUR_OF_DAY, 0);
c.set(Calendar.MINUTE, 0);
c.set(Calendar.SECOND, 0);
c.set(Calendar.MILLISECOND, 0);
// and get that as a Date
Date today = c.getTime();
// or as a timestamp in millisecondslong todayInMillis = c.getTimeInMillis();
// user-specified date which you are testing// let's say the components come from a form or somethingint year = 2011;
int month = 5;
int dayOfMonth = 20;
// reuse the calendar to set user specified date
c.set(Calendar.YEAR, year);
c.set(Calendar.MONTH, month);
c.set(Calendar.DAY_OF_MONTH, dayOfMonth);
// and get that as a Date
Date dateSpecified = c.getTime();
// test your conditionif (dateSpecified.before(today)) {
System.err.println("Date specified [" + dateSpecified + "] is before today [" + today + "]");
} else {
System.err.println("Date specified [" + dateSpecified + "] is NOT before today [" + today + "]");
}
Solution 3:
tl;dr
LocalDate
.parse( "2021-01-23" )
.isBefore(
LocalDate.now(
ZoneId.of( "Africa/Tunis" )
)
)
… or:
try
{
org.threeten.extra.LocalDateRangerange=
LocalDateRange.of(
LocalDate.of( "2021-01-23" ) ,
LocalDate.of( "2021-02-21" )
)
;
if( range.isAfter(
LocalDate.now( ZoneId.of( "Africa/Tunis" ) )
) { … }
else { … handle today being within or after the range. }
} catch ( java.time.DateTimeException e ) {
// Handle error where end is before start.
}
Details
The other answers ignore the crucial issue of time zone.
The other answers use outmoded classes.
Avoid old date-time classes
The old date-time classes bundled with the earliest versions of Java are poorly designed, confusing, and troublesome. Avoid java.util.Date/.Calendar and related classes.
java.time
- In Java 8 and later use the built-in java.time framework. See Tutorial.
- In Java 7 or 6, add the backport of java.time to your project.
- In Android, use the wrapped version of that backport.
LocalDate
For date-only values, without time-of-day and without time zone, use the LocalDate
class.
LocalDatestart= LocalDate.of( 2016 , 1 , 1 );
LocalDatestop= start.plusWeeks( 1 );
Time Zone
Be aware that while LocalDate
does not store a time zone, determining a date such as “today” requires a time zone. For any given moment, the date may vary around the world by time zone. For example, a new day dawns earlier in Paris than in Montréal. A moment after midnight in Paris is still “yesterday” in Montréal.
If all you have is an offset-from-UTC, use ZoneOffset
. If you have a full time zone (continent/region), then use ZoneId
. If you want UTC, use the handy constant ZoneOffset.UTC
.
ZoneIdzoneId= ZoneId.of( "America/Montreal" );
LocalDatetoday= LocalDate.now( zoneId );
Comparing is easy with isEqual
, isBefore
, and isAfter
methods.
boolean invalidInterval = stop.isBefore( start );
We can check to see if today is contained within this date range. In my logic shown here I use the Half-Open approach where the beginning is inclusive while the ending is exclusive. This approach is common in date-time work. So, for example, a week runs from a Monday going up to but not including the following Monday.
// Is today equal or after start (not before) AND today is before stop.boolean intervalContainsToday = ( ! today.isBefore( start ) ) && today.isBefore( stop ) ) ;
LocalDateRange
If working extensively with such spans of time, consider adding the ThreeTen-Extra library to your project. This library extends the java.time framework, and is the proving ground for possible additions to java.time.
ThreeTen-Extra includes an LocalDateRange
class with handy methods such as abuts
, contains
, encloses
, overlaps
, and so on.
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?
- Java SE 8, Java SE 9, Java SE 10, and later
- Built-in.
- Part of the standard Java API with a bundled implementation.
- Java 9 adds some minor features and fixes.
- Java SE 6 and Java SE 7
- Much of the java.time functionality is back-ported to Java 6 & 7 in ThreeTen-Backport.
- Android
- Later versions of Android bundle implementations of the java.time classes.
- For earlier Android (<26), the ThreeTenABP project adapts ThreeTen-Backport (mentioned above). See How to use ThreeTenABP….
Solution 4:
Android already has a dedicated class for this. Check DateUtils.isToday(long when)
Solution 5:
Using pure Java:
publicstatic boolean isToday(Date date){
Calendar today = Calendar.getInstance();
Calendar specifiedDate = Calendar.getInstance();
specifiedDate.setTime(date);
return today.get(Calendar.DAY_OF_MONTH) == specifiedDate.get(Calendar.DAY_OF_MONTH)
&& today.get(Calendar.MONTH) == specifiedDate.get(Calendar.MONTH)
&& today.get(Calendar.YEAR) == specifiedDate.get(Calendar.YEAR);
}
Post a Comment for "Check Date With Todays Date"