Solved

How to verify date in java?

Posted on 2008-10-17
4
555 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 500 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

Transaction Monitoring Vs. Real User Monitoring

Synthetic Transaction Monitoring Vs. Real User Monitoring: When To Use Each Approach? In this article, we will discuss two major monitoring approaches: Synthetic Transaction and Real User Monitoring.

Question has a verified solution.

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

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
When it comes to protecting Oracle Database servers and systems, there are a ton of myths out there. Here are the most common.
This video explains what a user managed backup is and shows how to take one, providing a couple of simple example scripts.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

728 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