Solved

Problem with leap years using SimpleDateFormat

Posted on 2000-03-04
5
1,203 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

Industry Leaders: 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

Suggested Solutions

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
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 learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…

733 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