0

In this program I got all ordinary years day approximately.. example by giving input 1 1 2014 I get output as wednesday.. but not on leap year example by giving input 1 1 2000 i'm get exact answer saturday.. but I get answer for 1 1 2000 is monday.. can anyone solve this...

Reference for Day of the date

//reference i made this program: http://easycalculation.com/funny/shortcuts/day.php
//gd = given date
//gm = given month
//gy = given year
//ym = year modulus (year % 100 to get last two digits)
//yp = year process (ym%4 to get remainder)
//wd = week days (0 for sunday, 1 for monday and so on...)
//d = day calculation (to get remainder from 0 to 6 .. which represents wd[0]
//for sunday so on)

import java.util.Scanner;
class dayof
{
public static void main(String arg[])
    {
    int gd,gm,gy,ym,yp,d;
    int[] m=new int[]{0,3,3,6,1,4,6,2,5,0,3,5};
    String[] wd=new String[]{"Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"};
    Scanner ip=new Scanner(System.in);
    System.out.println("Enter Date Month Year \"DD MM YYYY\"");
    gd=ip.nextInt();
    gm=ip.nextInt();
    gy=ip.nextInt();
    ym=gy%100;
    yp=ym%4;
    d=(gd+m[gm-1]+ym+yp+1)%7;
    System.out.print(wd[d]);
    }
}
4
  • 2
    Instead of using gd as variable name, and having a comment explaining that gd means "given date", why not use givenDate as variable name? Readability is VERY important. Use long, meaningful variable names. And read SimpleDateFormat api documentation. This question is being asked twice a day.
    – JB Nizet
    Jan 6, 2014 at 11:58
  • 1
    issue is with leap year logic. If year is divisible by 4 or 400 it's a leap year except when it's only divisible by 100.
    – asela38
    Jan 6, 2014 at 12:03
  • 1
    Why not to use existing libraries?
    – Alex
    Jan 6, 2014 at 12:11
  • It was assessment task given by my guide.. and he told me don't use existing libraries.. Jan 6, 2014 at 14:53

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.