Last active
June 2, 2019 20:24
-
-
Save Blizzardo1/681659935fd3698dc247784d0145a0c6 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package net.calendar.app; | |
import java.io.PrintStream; | |
import java.util.Calendar; | |
import java.util.Scanner; | |
public final class Program | |
{ | |
public Program() {} | |
public static void main(String[] args) | |
{ | |
Program p = new Program(); | |
int year = 0;int 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(7); | |
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(3); | |
} | |
public void printOneMonth(int month, int year, int start) { | |
int nextDay = start; | |
int totalDays = daysOfMonth(month, year, false); | |
System.out.printf("%10s%10s%10s%10s%10s%10s%10s", new Object[] { "Sun", "Mon", "Tue", | |
"Wed", "Thu", "Fri", "Sat" }); | |
println(); | |
for (int i = 1; i <= start; i++) { | |
System.out.printf("%10s", new Object[] { "" }); | |
} | |
for (int dates = 1; dates <= totalDays; dates++) { | |
System.out.printf("%10d", new Object[] { Integer.valueOf(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", new Object[] { | |
printMonthName(month), Integer.valueOf(year) }); | |
println(format); | |
} | |
switch (month) | |
{ | |
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; | |
case 4: | |
case 6: | |
case 9: | |
case 11: | |
if (!suppressMessage) | |
println("The Month of " + printMonthName(month) + " has " + 31 + | |
" days."); | |
return 30; | |
case 2: | |
if (isLeapYear(year)) { | |
if (!suppressMessage) | |
println("The Month of " + printMonthName(month) + " has " + | |
31 + " days."); | |
return 29; | |
} | |
if (!suppressMessage) | |
println("The Month of " + printMonthName(month) + " has " + | |
31 + " days."); | |
return 28; | |
} | |
return 0; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment