I have a date [format - dd/mm/yyyy]. I need to compare it with the current date so that I can execute this logic
if(end date is in the future) {
//logic
} else {
//logic
}
Can we do this in JAVA ?
I have a date [format - dd/mm/yyyy]. I need to compare it with the current date so that I can execute this logic
if(end date is in the future) {
//logic
} else {
//logic
}
Can we do this in JAVA ?
It looks like you are starting with a string so you can use SimpleDateFormat.parse to convert the String to a Date.
Then you can use the compareTo function on the Date class to see if it is in the past or not.
Something like the below (I havent compiled this...)
if (new SimpleDateFormat("dd/MM/yyy").parse(yourSring).compareTo(new Date()) > 0) {
...
}
If possible I would always use JodaTime thought instead of the Java Date and Calendar classes. It is much easier to use.
In general, use Joda Time:
DateTime.parse("...").isAfterNow();
You will probably want to pass an appropriate DateTimeFormatter
, such as DateTimeFormat.forPattern("d/M/y")
, as the second parameter.
use compareto method to compare two dates. check here in Date API
if(curreDate.compareTo(thisdate)>0) {
// do your logic
}
if(endDate.after(new Date())){}
just compare to a new Date it will be created a date time of now.