Solved

Problem with leap years using SimpleDateFormat

Posted on 2000-03-04
5
1,215 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
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.

717 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