Solved

How to verify date in java?

Posted on 2008-10-17
4
549 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
groupNoAdj 7 84
simple java question 3 43
Oracle Next Available Number 2 30
Problems using Provider=OraOLEDB.Oracle via VBScript/Classic ASP 5 44
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…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Via a live example, show how to take different types of Oracle backups using RMAN.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

929 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

11 Experts available now in Live!

Get 1:1 Help Now