Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1287
  • 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.

-Murali


                                import java.text.*;
                                 
                                public class date
                                {
                                        static SimpleDateFormat format = new SimpleDateFormat("MM/dd/yyyy");
                                 
                                        public static boolean isValidDate(String date)
                                        {
                                                format.setLenient(false);
                                                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));
                                        }
                                }
0
mk1234
Asked:
mk1234
1 Solution
 
kletkemanCommented:
I ran your code under Visual J++ 6.0 and it worked fine for me. The answer was "TRUE". OS is Windows 2000.
0
 
Ravindra76Commented:


WHat is Ur problem mk1234??
0
 
mk1234Author Commented:
ravindra

Read my code. It seems to think a leap year date such as 02/29/96 is not a valid date
0
 
kletkemanCommented:
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.
0
 
jadrek85Commented:
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);
    if(dt.getMonth()+1==parseMonth(date)){
      return true;
    }else
      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,

Jadhav
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

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