Archive
Posts Tagged ‘Calculate Week Day’
Java Program to find the Week Day of given date – Q14
January 7, 2009
Leave a comment
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):
Categories: Java
Calculate Week Day, Java Basic Programs




