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 “end” of the
year. Equation (3) corrects the date, and Equation (4) corrects the month accordingly.
Equation (5) calculates the number of leap years since the starting point, using the fact that every year
divisible by 4 is a leap year, unless it is divisible by 100 and not divisible by 400. Excel makes an
intentional error by allowing for February 29, 1900, a date that didn’t exist. It does so to maintain
compatibility with an earlier popular spreadsheet program, Lotus 123, which made this error
unintentionally (see
http://www.cpearson.com/excel/datetime.htm).
Equation (6) determines the number of days preceding the given month in a non-leap year, and then
Equation (7) adds on the day of the month, and the number of days preceding the year. The final day is d2.
Methods: