Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if a certain date is passed

Tags:

I have a string of the form "mm/yyyy" and I want to compare it against the date of the local system.

I have thought of either using a conversion table between my month and the MONTH field in Calendar, something like:

    Calendar cal = Calendar.getInstance();     String date = "07/2014";     String month = date.subString(0, 2);     int monthToCompare;     if (month.equals("01"))       monthToCompare = cal.JANUARY;     if (month.equals("02"))       monthToCompare = cal.FEBRUARY;     ... 

And then comparing manually with an if. I don't like it because I think is way too long for such a simple operation.

The other option I've thought of is getting the current Date() and using the before() method. That would mean translating my date to the Date format, but the easy methods to do it are deprecated, I must specify the number of milliseconds and I do not know how to easily do that (taking into consideration leap years, calendar corrections and so on since 1970).

like image 572
user2891462 Avatar asked Jun 12 '14 16:06

user2891462


People also ask

How do I find out past dates?

JavaScript Check if a Date is in the Pastvar dateInPast = function(firstDate, secondDate) { if (firstDate. setHours(0, 0, 0, 0) <= secondDate.

How do you know if a date is not past?

To check if a date is in the past:Use the Date() constructor to get the current date. Optionally set the time of the current date to midnight. Check if the date is less than the current date.

How do you check if a date has passed Python?

Use the isinstance built-in function to check if a variable is a datetime object in Python, e.g. if isinstance(today, datetime): . The isinstance function returns True if the passed in object is an instance or a subclass of the passed in class.


1 Answers

Using @Mifmif answer I finally solved the problem with:

if (new SimpleDateFormat("MM/yyyy").parse(date).before(new Date())) {     ... } 
like image 63
user2891462 Avatar answered Sep 26 '22 00:09

user2891462