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!


Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

New to Java, Help with Leap Year program

mhagen41mhagen41 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?


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

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

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);

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
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


Sign In or Register to comment.