Solved

How to verify date in java?

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

How to Create User-Defined Aggregates in Oracle Before we begin creating these things, what are user-defined aggregates?  They are a feature introduced in Oracle 9i that allows a developer to create his or her own functions like "SUM", "AVG", and…
Cursors in Oracle: A cursor is used to process individual rows returned by database system for a query. In oracle every SQL statement executed by the oracle server has a private area. This area contains information about the SQL statement and the…
This video shows how to Export data from an Oracle database using the Original Export Utility.  The corresponding Import utility, which works the same way is referenced, but not demonstrated.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

757 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now