33.13 (Day of week) Write a program that displays the day of the week for a given day, month, and year, as shown in Figure 33.36. The program lets the user select a day, month, and year, and click the Get Day of Week button to display the day of week. The Time field displays Future if it is a future day or Past otherwise. Use the Zeller’s congruence to find the day of the week (See Programming Exercise 3.21).
import java.text.SimpleDateFormat; import javax.faces.bean.ManagedBean; import javax.faces.bean.SessionScoped; import java.util.Date; @ManagedBean @SessionScoped public class Exercise13 { public int day = 1; public int month= 1; public int year=2017; public String dayOfWeek=""; public String time=""; public int getDay() { return day; } public void setDay(int data) { this.day = data; } public int getMonth() { return month; } public void setMonth(int data) { this.month = data; } public int getYear() { return year; } public void setYear(int data) { this.year = data; } public String getSolution() { return "YES"; } public String getdayOfWeek() { if ((month == 1)||(month == 2)) { month += 12; year--; } int yearOfCentury = year % 100; int century = year / 100; int cday = (day + (26 * (month + 1)) / 10 + yearOfCentury + yearOfCentury / 4 + century / 4 + 5 * century) % 7; switch(cday) { case 0: dayOfWeek="Saturday"; break; case 1: dayOfWeek="Sunday"; break; case 2: dayOfWeek="Monday"; break; case 3: dayOfWeek="Tuesday"; break; case 4: dayOfWeek="Wednesday"; break; case 5: dayOfWeek="Thursday"; break; case 6: dayOfWeek="Friday"; break; } return dayOfWeek; } public String gettime() { Date now = new Date(); SimpleDateFormat dateFormat=new SimpleDateFormat("yyyyMMdd"); try { Date date = dateFormat.parse(String.valueOf(year)+String.valueOf(month)+String.valueOf(day)); if( now.compareTo( date) < 0 ) { time="Future"; } else { time="Past"; } }catch(Exception e) { } return time; } public void setdayOfWeek() { } }
<?xml version = "1.0" encoding = "UTF-8"?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns = "http://www.w3.org/1999/xhtml" xmlns:f = "http://java.sun.com/jsf/core" xmlns:h = "http://java.sun.com/jsf/html"> <head> <title>Excercise 13</title> </head> <h:body> <h2>Day Of Week Calculator</h2> <hr /> <h:form> Day <h:selectOneMenu value = "#{exercise13.day}"> <f:selectItem itemValue = '1' itemLabel = '1' /> <f:selectItem itemValue = '2' itemLabel = '2' /> <f:selectItem itemValue = '3' itemLabel = '3' /> <f:selectItem itemValue = '4' itemLabel = '4' /> <f:selectItem itemValue = '5' itemLabel = '5' /> <f:selectItem itemValue = '6' itemLabel = '6' /> <f:selectItem itemValue = '7' itemLabel = '7' /> <f:selectItem itemValue = '8' itemLabel = '8' /> <f:selectItem itemValue = '9' itemLabel = '9' /> <f:selectItem itemValue = '10' itemLabel = '10' /> <f:selectItem itemValue = '11' itemLabel = '11' /> <f:selectItem itemValue = '12' itemLabel = '12' /> <f:selectItem itemValue = '13' itemLabel = '13' /> <f:selectItem itemValue = '14' itemLabel = '14' /> <f:selectItem itemValue = '15' itemLabel = '15' /> <f:selectItem itemValue = '16' itemLabel = '16' /> <f:selectItem itemValue = '17' itemLabel = '17' /> <f:selectItem itemValue = '18' itemLabel = '18' /> <f:selectItem itemValue = '19' itemLabel = '19' /> <f:selectItem itemValue = '20' itemLabel = '20' /> <f:selectItem itemValue = '21' itemLabel = '21' /> <f:selectItem itemValue = '22' itemLabel = '22' /> <f:selectItem itemValue = '23' itemLabel = '23' /> <f:selectItem itemValue = '24' itemLabel = '24' /> <f:selectItem itemValue = '25' itemLabel = '25' /> <f:selectItem itemValue = '26' itemLabel = '26' /> <f:selectItem itemValue = '27' itemLabel = '27' /> <f:selectItem itemValue = '28' itemLabel = '28' /> <f:selectItem itemValue = '29' itemLabel = '29' /> <f:selectItem itemValue = '30' itemLabel = '30' /> <f:selectItem itemValue = '31' itemLabel = '31' /> </h:selectOneMenu> Month <h:selectOneMenu value = "#{exercise13.month}"> <f:selectItem itemValue = '1' itemLabel = 'January' /> <f:selectItem itemValue = '2' itemLabel = 'February' /> <f:selectItem itemValue = '3' itemLabel = 'March' /> <f:selectItem itemValue = '4' itemLabel = 'April' /> <f:selectItem itemValue = '5' itemLabel = 'May' /> <f:selectItem itemValue = '6' itemLabel = 'June' /> <f:selectItem itemValue = '7' itemLabel = 'July' /> <f:selectItem itemValue = '8' itemLabel = 'August' /> <f:selectItem itemValue = '9' itemLabel = 'September' /> <f:selectItem itemValue = '10' itemLabel = 'October' /> <f:selectItem itemValue = '11' itemLabel = 'November' /> <f:selectItem itemValue = '12' itemLabel = 'December' /> </h:selectOneMenu> Year <h:inputText value="#{exercise13.year}" size="5"/> <h:commandButton value="Get Day of Week"/> <br /> <p> Day of the Week <h:outputLabel value="#{exercise13.dayOfWeek}"/> Time <h:outputLabel value="#{exercise13.time}"/> </p> <br /> </h:form> </h:body> </html>
No comments :
Post a Comment