Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Copyright Adonis S. Deliannis, 2012
- // All rights Reserved
- package net.calendar.app;
- import java.util.Calendar;
- import java.util.Scanner;
- public final class Program {
- public Program() {
- }
- /**
- * @param args
- */
- public static void main(String[] args) {
- Program P = new Program();
- int Year = 0, Start = 0;
- Scanner scin = new Scanner(System.in);
- P.print("Please enter a Year: ");
- Year = scin.nextInt();
- Calendar cal = Calendar.getInstance();
- cal.clear();
- cal.set(Year, 0, 0);
- Start = cal.get(Calendar.DAY_OF_WEEK);
- if(Start > 6)
- Start = 0;
- for (int Month = 1; Month <= 12; Month++) {
- P.printOneMonth(Month, Year, Start);
- Start = (Start + P.daysOfMonth(Month, Year, true)) % 7;
- P.print("Enter random text and press enter...\r\n");
- scin.next();
- P.println();
- }
- }
- public void println() {
- System.out.println();
- }
- public void println(Object O) {
- System.out.println(O);
- }
- public void print(Object O) {
- System.out.print(O);
- }
- public boolean isLeapYear(int Year) {
- return (Year % 4) == 0 && (Year % 100) != 0 || (Year % 400) == 0;
- }
- public String printMonthName(int Month) {
- switch (Month) {
- case 1:
- return "January";
- case 2:
- return "February";
- case 3:
- return "March";
- case 4:
- return "April";
- case 5:
- return "May";
- case 6:
- return "June";
- case 7:
- return "July";
- case 8:
- return "August";
- case 9:
- return "September";
- case 10:
- return "October";
- case 11:
- return "November";
- case 12:
- return "December";
- }
- return "Not a valid month";
- }
- public int printWeekTitle(int Month, int Day, int Year) {
- Calendar cal = Calendar.getInstance();
- cal.clear();
- cal.set(Year, Month, Day);
- return cal.get(Calendar.WEEK_OF_YEAR);
- }
- public void printOneMonth(int month, int year, int start) {
- int nextDay = start;
- int totalDays = daysOfMonth(month, year, false);
- // Write code to call method to print month name
- // Write code to call method to print week title
- // Write code to call method to move cursors to the right starting
- // position.
- System.out.printf("%10s%10s%10s%10s%10s%10s%10s", "Sun", "Mon", "Tue",
- "Wed", "Thu", "Fri", "Sat");
- println();
- for (int i = 1; i <= start; i++)
- System.out.printf("%10s", "");
- for (int dates = 1; dates <= totalDays; dates++) {
- System.out.printf("%10d", dates);
- nextDay = (nextDay + 1) % 7;
- if (nextDay == 0)
- println();
- }
- println();
- }
- public int daysOfMonth(int Month, int Year, boolean SuppressMessage) {
- if (!SuppressMessage) {
- String format = String.format(
- "Calculating the days of the Month of %s in %s",
- printMonthName(Month), Year);
- println(format);
- }
- switch (Month) {
- // January, March, May, July, August, October, December
- case 1:
- case 3:
- case 5:
- case 7:
- case 8:
- case 10:
- case 12:
- if (!SuppressMessage)
- println("The Month of " + printMonthName(Month) + " has " + 31
- + " days.");
- return 31;
- // April, June, September, November
- case 4:
- case 6:
- case 9:
- case 11:
- if (!SuppressMessage)
- println("The Month of " + printMonthName(Month) + " has " + 30
- + " days.");
- return 30;
- // February
- case 2:
- if (isLeapYear(Year)) {
- if (!SuppressMessage)
- println("The Month of " + printMonthName(Month) + " has "
- + 29 + " days.");
- return 29;
- } else {
- if (!SuppressMessage)
- println("The Month of " + printMonthName(Month) + " has "
- + 28 + " days.");
- return 28;
- }
- }
- return 0;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement