ISourceCode

Make the frequent cases fast and the rare case correct

What was the day when man first landed on moon?

Everyone remembers july 20th 1969 , but can you quickly find the day of the week?

At 02:39 UTC on Monday July 21 (10:39pm EDT, Sunday July 20), 1969, Armstrong opened the hatch, and at 02:51 UTC began his descent to the Moon’s surface.

import java.util.Scanner;
import java.util.Arrays;
public class DayFinder
{
        public static void main(String args[]){

        Scanner input=new Scanner(System.in);
        int monthend[]={0,31,28,31,30,31,30,31,31,30,31,30,31};
        String  dayname[]={"monday", "tuesday", "wednesday", "thursday", "friday", "saturday", "sunday"};

        int year,month,day,timer;
        System.out.println("Enter the date yyyy mm dd");
        year=input.nextInt();
        month=input.nextInt();
        day=input.nextInt();

        timer=(year-1)*365;
        for (int i=1;i< year;i++)
        {
                if ((i%4==0&&i%100!=0)||(i%400==0))
                {
                        timer++;
                }
        }
        if ((year%4==0&&year%100!=0)||(year%400==0))
        {
                monthend[2]++;
        }
        for (int i=1;i< month;i++)
        {
                timer+=monthend[i];
        }
        timer+=day+6;
        System.out.println("Day is :"+dayname[timer%7]);
        }
}

OUTPUT:
> java DayFinder
Enter the date yyyy mm dd
1969 07 20
Day is :sunday

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: