New to Java, Help with Leap Year program - Programmers Heaven

#### Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

# New to Java, Help with Leap Year program

Posts: 2Member
Hey everyone, I'm new here and I'm stuck on an a program that asks the user to input a date (yyyy-mm-dd). My program will output the amount of days since January 01 1900.
Anyways, everything was running good up until my leap year conversion equation. I have to take the integer part of a few of the numbers, and I'm not sure if I am doing it correctly. Can anyone help me out?

-Thanks

import javax.swing.*;
import java.util.Date ;
import java.util.Scanner;

public class Days{
public static void main (String args[]){

//variables
String input;
Scanner source;
int year;
int month;
int day;
int year2;
int earlyMonth;
int year3;
int month2;
int leapYear;
int day1;
int day2;

input = JOptionPane.showInputDialog("Enter a date (yyyy-mm-dd)");
source = new Scanner (input);
source.useDelimiter("-");

year = source.nextInt( );
month = source.nextInt( );
day = source.nextInt( );

year2 = year - 1900;
earlyMonth = (int)Math.floor(14-month)/12.;
year3 = year2 - earlyMonth;
month2 = month + 12*earlyMonth;

leapYear = 1 + Math.min(year3,0) +(int)Math.floor(year3)/4.) - (int)Math.floor(year3/100.) + (int)Math.floor(year3+300)/400 ;
}
}

This is from my professor:

Formula: Use the following formulas to determine the day:
1) year2 = year - 1900
2) earlyMonth = | (14-month)/12 |
3) year3 = year2 - earlyMonth
4) month2 = month + 12*earlyMonth
5) leapYear = 1 + min(year3,0) + | (year3)/4) | - | (year3/100) | + | (year3+300)/400 |
6) day1 = -1.63 + | (month2-1)*30.6 |
7) day2 = day + year3*365 + leapYear + day1

Note 1: the notation
| ... |
in these equations means to take the integer part of the result.
Note 2: the cryptic names used in these mathematical equations are NOT suitable for use in a computer
program.
Equation (1) determines the year offset from the starting point.
Equation (2) is an early-month correction. It is easier to do the calculation if we think of January and
February as the 13th and 14th months of the previous year, so the leap day is added on at the

• Posts: 198Member
Hey,
I think the int casts used will do the job because of the way the methods you are using work. If something doesn't seem to be working right, debug individual components using print statements, ie.
[code]
int year3 = 27;

ps. I think there is a problem with the formula for day1 because of the -1.63 will cause a double value that is not directly usable as an integer.

g.l. se

• Posts: 2Member
thanks! My friend helped me out and we found the error. I was missing a few brackets in the leapYear formula.