Using Java validate date is not in the future

Hello,

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"
cgray1223Asked:
Who is Participating?

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

x
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
http://www.java2s.com/Tutorial/Java/0040__Data-Type/SimpleDateFormat.htm

convert that date object to milliseconds
http://www.java-examples.com/convert-date-milliseconds-example

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
0
for_yanCommented:


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();

if(dd2.before(dd1)){
then your s1 date is in the future
}
0

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");
df.setTimeZone(TimeZone.getTimeZone("GMT"));

Date testDate = df.parse(testDateStr);

if(testDate.after(new Date()))
{
    System.out.println("testDate is later than NOW");
}
else
{
    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.
0
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
Java

From novice to tech pro — start learning today.