Using Java validate date is not in the future


How can I tell if a date is in the following format is in the future as compared to the current GMT system time

"MM/dd/yy HH:mm:ss"
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Gurvinder Pal SinghCommented:
using SimpleDateFormat, you will convert that time to a Date object

convert that date object to milliseconds

Now, get the current Date time's milliseconds by doing
Date date=new Date() ;    
 System.out.println("Today is " +date.getTime())

if the current time's milliseconds' value is smaller, then the input date was of future time

String s1 = "03/27/11 13:05:23");
SimpleDateFormat f = new SimpleDateFormat("MM/dd/yy HH:mm:ss z" );

java.util.Date dd1 = f.parse(s1 + " GMT", new ParsePosition(0));

java.util.Date dd2 = new java.util.Date();

then your s1 date is in the future

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
mccarlIT Business Systems Analyst / Software DeveloperCommented:
You mention that you are comparing GMT time, so you may need something along these lines...

String testDateStr = "03/29/11 04:45:13";

DateFormat df = new SimpleDateFormat("MM/dd/yy HH:mm:ss");

Date testDate = df.parse(testDateStr);

if(testDate.after(new Date()))
    System.out.println("testDate is later than NOW");
    System.out.println("testDate is earlier than NOW");

Open in new window

Note: Line 5 is where you tell the parser to interpret that date that is in the String as a GMT date. Also using the "after" method of Date (on line 9) makes it a bit more obvious exactly what you are checking for.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.