Advertisements
Home > Java > Java Program to find the Week Day of given date – Q14

Java Program to find the Week Day of given date – Q14


Q14: Java Program to find the Week Day of given date

import javax.swing.*;

class cal{
	public static void main(String args[]){
		int dd, mm, yy, t_y, odd=0;
		String inp;
		
		inp = JOptionPane.showInputDialog("Enter a Day (dd): ");
		dd = Integer.parseInt(inp);
		
		inp = JOptionPane.showInputDialog("Enter a Day (mm): ");
		mm = Integer.parseInt(inp);
		
		inp = JOptionPane.showInputDialog("Enter a Day (yy): ");
		yy = Integer.parseInt(inp);
		
		if( ((dd <= 0) || (dd >= 32)) &&
			((mm <= 0) || (mm >= 13)) &&
			(yy <= 0) ){
			System.out.println("Enter a Valid date.");
			System.exit(0);
			}
		
		t_y = (yy-1) % 400;
		
		if(t_y == 0)
			odd = 0;
		if(t_y >= 300 && t_y < 400){
			odd = 1;
			t_y -= 300;
			}
		if(t_y >= 200 && t_y < 300){
			odd = 3;
			t_y -= 200;
			}
		if(t_y >= 100 && t_y < 200){
			odd = 5;
			t_y -= 100;
			}
		
		odd += t_y/2;
		t_y -= t_y/4;
		odd += t_y;
		
		for(int i=0; i<mm-1; i++)
			odd += DaysThisMonth(i, yy);
		
		odd += dd;
		
		odd = odd % 7;
		
		System.out.println("\n Day on Date " + dd + "/" + mm + "/" 
     + yy + " is: " + week[odd]);
	}
	
	public static String week[] = { "Sunday", "Monday", "Tuesday", 
  "Wednesday", "Thursday",
  "Friday", "Saturday" };
	
	public static int months[] = { 31, 29, 31, 30, 31, 30, 31,
							 31, 30, 31, 30, 31 };
	
	public static int DaysThisMonth(int m, int y){
		if (m != 1)
			return months[m];
		
		if ((y % 4) != 0)		// Not leap year
			return 28;
		if ((y % 100) != 0)		// It is leap year
			return 29;
		if ((y % 400) != 0)		// Not leap year
			return 28;
		return 29;			// It is leap year
	}
}

… from College notes (BCA/MCA assignments):


Advertisements
  1. No comments yet.
  1. No trackbacks yet.

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: