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
Solved

Problem with leap years using SimpleDateFormat

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

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

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
V.simple question about Android packages and libraries 1 29
ejb entity bean example 2 17
web project error add remove 1 29
Setup GlassFish 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)
INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
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 will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

828 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