?
Solved

Detect Leap Year

Posted on 2011-10-10
5
Medium Priority
?
235 Views
Last Modified: 2012-05-12
How do I detect if its a leap year in Java and change the number of days in a month accordingly ?
0
Comment
Question by:abuyusuf35
  • 3
5 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 36942923
Calendar:
Calendar c = Calendar.getInstance();
boolean leap = c.isLeapYear();

Open in new window

0
 
LVL 86

Expert Comment

by:CEHJ
ID: 36942937
Oops
GrgorianCalendar c = new GregorianCalendar();
boolean leap = c.isLeapYear();

Open in new window

0
 
LVL 47

Accepted Solution

by:
for_yan earned 2000 total points
ID: 36942983

This deermines niumber of days in a month:
http://www.exampledepot.com/egs/java.util/GetDaysInMonth.html


// Create a calendar object of the desired month
Calendar cal = new GregorianCalendar(1999, Calendar.FEBRUARY, 1);

// Get the number of days in that month
int days = cal.getActualMaximum(Calendar.DAY_OF_MONTH); // 28

// Try month in a leap year
cal = new GregorianCalendar(2000, Calendar.FEBRUARY, 1);
days = cal.getActualMaximum(Calendar.DAY_OF_MONTH);     // 29

Open in new window

http://www.dreamincode.net/forums/topic/67757-calculate-the-number-of-days-in-a-month/
0
 
LVL 37

Expert Comment

by:zzynx
ID: 36947685
>> ... and change the number of days in a month accordingly?
What exactly do you mean by that?
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 37029933
abuyusuf35, can you tell me why you ignored my first comment?
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
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…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Suggested Courses
Course of the Month15 days, 17 hours left to enroll

850 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