Solved

Problem with leap years using SimpleDateFormat

Posted on 2000-03-04
5
1,176 Views
Last Modified: 2012-06-27
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
Comment
Question by:mk1234
5 Comments
 

Expert Comment

by:kletkeman
ID: 2584063
I ran your code under Visual J++ 6.0 and it worked fine for me. The answer was "TRUE". OS is Windows 2000.
0
 
LVL 7

Expert Comment

by:Ravindra76
ID: 2584607


WHat is Ur problem mk1234??
0
 

Author Comment

by:mk1234
ID: 2587717
ravindra

Read my code. It seems to think a leap year date such as 02/29/96 is not a valid date
0
 

Expert Comment

by:kletkeman
ID: 2589566
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
 
LVL 1

Accepted Solution

by:
jadrek85 earned 100 total points
ID: 2590426
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
How Complex Is This Java Course ? 9 63
firstChar challenge 13 85
Python Assistance 7 36
Cipher Configuration on Apache HTTPD 4 22
For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
This video teaches viewers about errors in exception handling.

759 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

19 Experts available now in Live!

Get 1:1 Help Now