Wed Sep 22 2021
Find Day
Java Programming984 views
File Name: find-weekday.java
import java.io.*;
import java.text.SimpleDateFormat;
import java.util.Scanner;
class finddayname {
finddayname(String fulldate) {
try {
boolean dateValid = dateValidate(fulldate);
if(dateValid) {
SimpleDateFormat dformat = new SimpleDateFormat("dd/MM/yyyy");
java.util.Date date = dformat.parse(fulldate);
dformat.applyPattern("EEEEE");
String day = dformat.format(date);
/* Compare string using 'equals' */
if(day.equals("Saturday") || day.equals("Sunday"))
System.out.println(day+" : Weekend");
else
System.out.println(day+" : Weekday");
}
else
System.out.println("Invalid Date!");
}
/* Catch exceptional error from 'try' block */
catch(Exception e) {
System.out.println("Invalid Date Format!!!");
}
}
boolean dateValidate(String date) {
/* Split string into array */
String dateArray[] = date.split("/");
int day = Integer.parseInt(dateArray[0]);
int month = Integer.parseInt(dateArray[1]);
int year = Integer.parseInt(dateArray[2]);
boolean leapYear = false;
if((year % 4 == 0) && (year % 100 != 0) || (year % 400 == 0))
leapYear = true;
if(year > 2099 || year < 1900)
return false;
if(month < 13) {
if(month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12) {
if(day > 31)
return false;
}
else if(month == 4 || month == 6 || month == 9 || month == 11) {
if(day > 30)
return false;
}
else if(leapYear == true && month == 2) {
if(day > 29)
return false;
}
else if(leapYear == false && month == 2) {
if(day > 28)
return false;
}
return true;
}
else
return false;
}
}
class weekdayname {
public static void main(String args[ ]) {
System.out.println("Enter the date(dd/mm/yyyy):");
Scanner input = new Scanner(System.in);
new finddayname(input.nextLine());
}
}
/* Output */
Enter the date(dd/mm/yyyy):
22/09/2014
Monday : Weekday
Author:Geekboots