?
Solved

How to verify date in java?

Posted on 2008-10-17
4
Medium Priority
?
557 Views
Last Modified: 2013-12-07
I have to verify date is valid in mm/dd/yyyy formate after submittingbutton.
how do i get  that?

Thanks
0
Comment
Question by:ANISHKA1
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 20

Expert Comment

by:chaitu chaitu
ID: 22740240
0
 
LVL 12

Expert Comment

by:basav_com
ID: 22740281
You need to use DateFormat class's parse method for this.
DateFormat df=DateFormat.getDateInstance();
myDate = df.parse(above date string);

If you get parse exception in the above code, then the date is not in the proper format.

Check this for more details:
http://java.sun.com/j2se/1.4.2/docs/api/java/text/DateFormat.html

0
 
LVL 20

Accepted Solution

by:
chaitu chaitu earned 2000 total points
ID: 22740291
     public static boolean validDate( String date, String format )
      {
          try
          {
              SimpleDateFormat sdf = new SimpleDateFormat( format ) ;
              sdf.setLenient( false ) ;
              sdf.parse( date ) ;
              return true ;
          } catch (Exception e)
          {
              return false ;
          }
      }

public static void main(String[] args) {
      if ( validDate( "02/02/2006", "MM/dd/yyyy" ) )
      {
         System.out.println("valid");
      }

}
      
0
 

Author Closing Comment

by:ANISHKA1
ID: 31507105
It is perfect.
0

Featured Post

Want to be a Web Developer? Get Certified Today!

Enroll in the Certified Web Development Professional course package to learn HTML, Javascript, and PHP. Build a solid foundation to work toward your dream job!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

How to Unravel a Tricky Query Introduction If you browse through the Oracle zones or any of the other database-related zones you'll come across some complicated solutions and sometimes you'll just have to wonder how anyone came up with them.  …
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
This video teaches viewers about errors in exception handling.
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses
Course of the Month8 days, 22 hours left to enroll

764 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question