Solved

How to verify date in java?

Posted on 2008-10-17
4
551 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
  • 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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How to get all the API from website? 11 91
type mismatch (Object[] to double[] 4 24
eclipse buid path vs tomcat lib path 10 24
ORA-02288: invalid OPEN mode 2 31
Have you ever had to make fundamental changes to a table in Oracle, but haven't been able to get any downtime?  I'm talking things like: * Dropping columns * Shrinking allocated space * Removing chained blocks and restoring the PCTFREE * Re-or…
Configuring and using Oracle Database Gateway for ODBC Introduction First, a brief summary of what a Database Gateway is.  A Gateway is a set of driver agents and configurations that allow an Oracle database to communicate with other platforms…
This video shows how to Export data from an Oracle database using the Datapump Export Utility.  The corresponding Datapump Import utility is also discussed and demonstrated.
This video teaches viewers about errors in exception handling.

809 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