• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1317
  • Last Modified:

Problem with leap years using SimpleDateFormat

Why does my code doesn't work for 'leap' years? Could anyone suggest a work around? Please post sample code. Thanks.


                                import java.text.*;
                                public class date
                                        static SimpleDateFormat format = new SimpleDateFormat("MM/dd/yyyy");
                                        public static boolean isValidDate(String date)
                                                return format.parse(date,new ParsePosition(0)) == null ? false : true;
                                        public static void main(String[] args)
                                                String day1 = "02/29/1996";
                                                System.out.println(day1 + " is valid: " + isValidDate(day1));
1 Solution
I ran your code under Visual J++ 6.0 and it worked fine for me. The answer was "TRUE". OS is Windows 2000.

WHat is Ur problem mk1234??
mk1234Author Commented:

Read my code. It seems to think a leap year date such as 02/29/96 is not a valid date
Since I've already posted that your code works fine under Windows 2000 with Visual J++, it would be very helpful if you would follow up with your particular configuration and toolset, as you apparently are not getting the correct result. That way, someone who uses the same setup could try to answer the question.
Hi Murali,

Here is the code, which validates the date for leap year. enter different year, month and day to check for leap year.

//Start code
import java.text.*;
import java.util.*;

public class LeapYear{
  public static boolean isValidDate(String date){
    Date dt=new Date(date);
      return true;
      return false;

  public static void main(String[] args){
    String day1 = "02/29/1996";  // change the date here
    System.out.println(day1 + " is valid: " + isValidDate(day1.trim()));
    for(;;);  // this is to hold the window
  public static int parseMonth(String dt){
    return Integer.parseInt(dt.substring(0,dt.indexOf("/",0)));
// End code

Good Luck,

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now