Created
November 17, 2011 11:47
-
-
Save gautamk/1372982 to your computer and use it in GitHub Desktop.
A problematic Calendar Generator
This file contains 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 ab2pdox; | |
import java.util.Calendar; | |
import java.util.GregorianCalendar; | |
/** | |
* For generating a string containing a calender. | |
* Also contains useful enums to manage | |
*/ | |
public class jcal { | |
public static void main(String []args){ | |
System.out.println(getCalender(new GregorianCalendar( | |
2000,//Integer.parseInt(args[0]), | |
Calendar.DECEMBER,//Integer.parseInt(args[1]), | |
5 | |
))); | |
} | |
private static String getCalenderHeader(GregorianCalendar gcal){ | |
int month = gcal.get(GregorianCalendar.MONTH), | |
year = gcal.get(GregorianCalendar.YEAR); | |
/* | |
* An Enum indicating January ,February .... December | |
*/ | |
MonthOfTheYear m = MonthOfTheYear.getMonthOfTheYear(month); | |
return " " + m + " " + year + "\n" + | |
"Su Mo Tu We Th Fr Sa\n"; | |
} | |
public static int getDayOfTheWeek(int M, int D, int Y) { | |
int y = Y - (14 - M) / 12; | |
int x = y + y/4 - y/100 + y/400; | |
int m = M + 12 * ((14 - M) / 12) - 2; | |
int d = (D + x + (31*m)/12) % 7; | |
return d; | |
} | |
public static String getCalender(GregorianCalendar gcal){ | |
String calender = new String(); | |
calender += getCalenderHeader(gcal); | |
int year = gcal.get(GregorianCalendar.YEAR); | |
MonthOfTheYear month = MonthOfTheYear.getMonthOfTheYear( | |
gcal.get(GregorianCalendar.MONTH) ); | |
int numberOfDaysInTheMonth = month.numberOfDays(year); | |
int startingDay =getDayOfTheWeek( | |
gcal.get(Calendar.MONTH), | |
1, | |
gcal.get(Calendar.YEAR) ); | |
/* | |
* Print the days before the month starts, as blank | |
*/ | |
for (int i = 0; i < startingDay - 1; i++) | |
calender+=" "; | |
for (int i = 1; i <=numberOfDaysInTheMonth; i++) { | |
calender += ( (i<10?" ":"") + i + " "); | |
if ( ( (i + startingDay) % 7 == 0 ) || (i == numberOfDaysInTheMonth)) | |
calender+="\n"; | |
} | |
return calender; | |
} | |
/** | |
* An enum representing the day of the week with some useful methods. | |
*/ | |
public static enum DayOfTheWeek { | |
SUNDAY ("Sunday", Calendar.SUNDAY), | |
MONDAY ("Monday", Calendar.MONDAY), | |
TUESDAY ("Tuesday", Calendar.TUESDAY), | |
WEDNESDAY ("Wednesday", Calendar.WEDNESDAY), | |
THURSDAY ("Thursday", Calendar.THURSDAY), | |
FRIDAY ("Friday", Calendar.FRIDAY), | |
SATURDAY ("Saturday", Calendar.SATURDAY); | |
/** | |
* A String representing the day of the week | |
*/ | |
private final String dayOfTheWeek; | |
/** | |
* An Integer representing the day of the week | |
*/ | |
private final int day_of_the_week; | |
DayOfTheWeek(String dayOfTheWeek, int day_of_the_week) { | |
this.dayOfTheWeek = dayOfTheWeek; | |
this.day_of_the_week = day_of_the_week; | |
} | |
@Override | |
public String toString(){ | |
return dayOfTheWeek; | |
} | |
/** | |
* Get the day of the week in Integer form | |
* @return An integer from 1 to 7 representing the day of the week | |
*/ | |
public int toInt(){ | |
return day_of_the_week; | |
} | |
/** | |
* Get a value of type DayOfTheWeek from an Integer | |
* @param dayOfTheWeek An integer from 1 to 7 representing the day of the week | |
* For example 1 returns Sunday , 2 returns Monday etc. | |
* @return A value of enum type DayOfTheWeek | |
* @throws IllegalArgumentException if the given integer is not from 1 to 7 | |
* @see ab2pdox.jcal.DayOfTheWeek | |
*/ | |
public static DayOfTheWeek getDayOfTheWeek(int dayOfTheWeek)throws IllegalArgumentException{ | |
switch(dayOfTheWeek){ | |
case Calendar.SUNDAY: return SUNDAY; | |
case Calendar.MONDAY: return MONDAY; | |
case Calendar.TUESDAY: return TUESDAY; | |
case Calendar.WEDNESDAY:return WEDNESDAY; | |
case Calendar.THURSDAY: return THURSDAY; | |
case Calendar.FRIDAY: return FRIDAY; | |
case Calendar.SATURDAY: return SATURDAY; | |
default: | |
throw new IllegalArgumentException("Invalid Day of the week"); | |
} | |
} | |
/** | |
* | |
* @param day The day of the week as a string (Not Case Sensitive) | |
* like "Monday" , "Tuesday" etc | |
* | |
* @return A value of enum type DayOfTheWeek | |
* | |
* @throws IllegalArgumentException if the string is not a day of the week | |
* | |
* @see ab2pdox.jcal.DayOfTheWeek | |
* | |
*/ | |
public static DayOfTheWeek getDayOfTheWeek(String day) | |
throws IllegalArgumentException { | |
for(int i=Calendar.SUNDAY; | |
i<=Calendar.SATURDAY; | |
i++) { | |
DayOfTheWeek d = DayOfTheWeek.getDayOfTheWeek(i); | |
if(day.equalsIgnoreCase(d.toString())){ | |
return d; | |
} | |
} | |
throw new IllegalArgumentException("The given string is not a day"); | |
} | |
} | |
/** | |
* An enum representing the month of the year | |
*/ | |
public static enum MonthOfTheYear{ | |
JANUARY ("January", Calendar.JANUARY, 31), | |
FEBRUARY ("February", Calendar.FEBRUARY, 28), | |
MARCH ("March", Calendar.MARCH, 31), | |
APRIL ("April", Calendar.APRIL, 30), | |
MAY ("May", Calendar.MAY, 31), | |
JUNE ("June", Calendar.JUNE, 30), | |
JULY ("July", Calendar.JULY, 31), | |
AUGUST ("August", Calendar.AUGUST, 31), | |
SEPTEMBER ("September", Calendar.SEPTEMBER, 30), | |
OCTOBER ("October", Calendar.OCTOBER, 31), | |
NOVEMBER ("November", Calendar.NOVEMBER, 30), | |
DECEMBER ("December", Calendar.DECEMBER, 31); | |
/** | |
* A String representing a month of the year | |
*/ | |
private final String MONTH_STRING ; | |
/** | |
* A Integer representing a month of the year | |
*/ | |
private final int MONTH_INT; | |
private final int DAYS_IN_THE_MONTH; | |
MonthOfTheYear(String monthString, int monthInt , int daysInTheMonth){ | |
this.MONTH_STRING = monthString; | |
this.MONTH_INT = monthInt; | |
this.DAYS_IN_THE_MONTH = daysInTheMonth; | |
} | |
@Override | |
public String toString(){ | |
return MONTH_STRING ; | |
} | |
public int toInt(){ | |
return MONTH_INT; | |
} | |
/** | |
* This function | |
* @return The number of days in the current month | |
* of the current year | |
*/ | |
public int numberOfDays(){ | |
return numberOfDays( | |
new GregorianCalendar().get( | |
GregorianCalendar.YEAR) ); | |
} | |
/** | |
* | |
* @param YEAR | |
* @return The number of days in the current month in a particular year. | |
*/ | |
public int numberOfDays(final int YEAR){ | |
GregorianCalendar cal = new GregorianCalendar(YEAR,MONTH_INT,1); | |
int numberOfDays = DAYS_IN_THE_MONTH; | |
if((MONTH_INT == 2) && | |
( cal.isLeapYear( | |
cal.get(GregorianCalendar.YEAR) ) ) ) { | |
numberOfDays+=1; | |
assert numberOfDays==29; | |
} | |
return numberOfDays; | |
} | |
/** | |
* Get Month of the year from an Integer | |
* @param Month An Integer from 1 to 12 representing a month of the year | |
* @return MonthOfTheYear | |
* @throws IllegalArgumentException if the given integer is not from 1 to 12 | |
*/ | |
public static MonthOfTheYear getMonthOfTheYear(int Month)throws IllegalArgumentException { | |
switch (Month){ | |
case Calendar.JANUARY: return JANUARY ; | |
case Calendar.FEBRUARY: return FEBRUARY ; | |
case Calendar.MARCH: return MARCH ; | |
case Calendar.APRIL: return APRIL ; | |
case Calendar.MAY: return MAY ; | |
case Calendar.JUNE: return JUNE ; | |
case Calendar.JULY: return JULY ; | |
case Calendar.AUGUST: return AUGUST; | |
case Calendar.SEPTEMBER:return SEPTEMBER; | |
case Calendar.OCTOBER: return OCTOBER; | |
case Calendar.NOVEMBER: return NOVEMBER; | |
case Calendar.DECEMBER: return DECEMBER; | |
default:throw new IllegalArgumentException("The given integer is not a month"); | |
} | |
} | |
/** | |
* Convert a string to enum MonthOfTheYear | |
* @param Month A string representing a month of the year | |
* @return MonthOfTheYear | |
* @throws IllegalArgumentException if the given string is not a month | |
*/ | |
public static MonthOfTheYear getMonthOfTheYear(String Month)throws IllegalArgumentException{ | |
for(int i=Calendar.JANUARY; | |
i<=Calendar.DECEMBER; | |
i++) { | |
MonthOfTheYear m = MonthOfTheYear.getMonthOfTheYear(i); | |
if(Month.equalsIgnoreCase(m.toString())){ | |
return m; | |
} | |
} | |
throw new IllegalArgumentException("The String is not a valid Month") ; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment