import java.io.PrintWriter;
import java.text.DateFormatSymbols;
import java.util.Date;
import java.util.Dictionary;
import java.util.Hashtable;
import java.util.Locale;
public class Calendar
{
// following is identical to JSP
private static final Dictionary dates = new Hashtable();
static
{
// certainly not an exhaustive list,
// but at least one per month
dates.put("1/27", "Wolfgang Amadeus Mozart, 1756");
dates.put("2/3", "Felix Mendelssohn, 1809");
dates.put("2/23", "Georg Friedrich Handel, 1685");
dates.put("3/21", "Johann Sebastian Bach, 1685");
dates.put("3/25", "Bela Bartok, 1881");
dates.put("3/31", "Franz Joseph Haydn, 1732");
dates.put("4/27", "Sergei Prokofiev, 1891");
dates.put("5/5", "Johannes Brahms, 1833");
dates.put("6/2", "Sir Edward Elgar, 1857");
dates.put("6/15", "Edward Grieg, 1843");
dates.put("7/3", "Leos Janacek, 1854");
dates.put("7/7", "Gustav Mahler, 1860");
dates.put("8/22", "Claude Debussy, 1862");
dates.put("9/4", "Anton Bruckner, 1824");
dates.put("9/8", "Anton Dvorak, 1841");
dates.put("9/26", "George Gershwin, 1898");
dates.put("10/20", "Charles Ives, 1874");
dates.put("10/22", "Franz List, 1811");
dates.put("11/18", "Carl Weber, 1786");
dates.put("12/10", "Cesar Franck, 1822");
dates.put("12/15", "Ludwig van Beethoven, 1770");
dates.put("12/30", "Dmitri Kabalevsky, 1904");
}
// above is identical to JSP
public static void main(String[] args)
{
PrintWriter writer = new PrintWriter(System.out, true);
// get requested month/year
int calendarMonth;
int calendarYear;
int month = args.length == 0
? Integer.MAX_VALUE
: Integer.parseInt(args[0]);
int year = args.length == 0
? Integer.MAX_VALUE
: Integer.parseInt(args[1]);
output(writer, month, year);
writer.flush();
writer.close();
}
public static void output(PrintWriter out, int calendarMonth, int calendarYear)
{
// following is almost identical to JSP
// differences:
// - input argument handling
// - links are removed
Locale locale = Locale.getDefault();
String event;
java.util.Calendar calendar = java.util.Calendar.getInstance(locale);
int firstDayOfWeek = calendar.getFirstDayOfWeek();
DateFormatSymbols dateFormatSymbols = new DateFormatSymbols(locale);
// weekday names is one-based; [0] is empty
String[] dayNames;
{
String[] originalDayNames = dateFormatSymbols.getShortWeekdays();
dayNames = new String[originalDayNames.length-1];
System.arraycopy(
originalDayNames, firstDayOfWeek,
dayNames, 0, dayNames.length-(firstDayOfWeek-1));
if ( firstDayOfWeek != 1 )
{
System.arraycopy(
originalDayNames, 1,
dayNames, dayNames.length-(firstDayOfWeek-1), firstDayOfWeek-1);
}
}
String[] monthNames = dateFormatSymbols.getMonths();
// get info about today
int thisDay = calendar.get(java.util.Calendar.DAY_OF_MONTH);
int thisMonth = calendar.get(java.util.Calendar.MONTH);
int thisYear = calendar.get(java.util.Calendar.YEAR);
if ( calendarMonth == Integer.MAX_VALUE )
{
calendarMonth = thisMonth;
}
if ( calendarYear == Integer.MAX_VALUE )
{
calendarYear = thisYear;
}
String monthName = monthNames[calendarMonth];
calendar.set(java.util.Calendar.DAY_OF_MONTH, 1);
calendar.set(java.util.Calendar.MONTH, calendarMonth);
calendar.set(java.util.Calendar.YEAR, calendarYear);
// what weekday is the first day of the month?
int firstWeekdayOfMonth =
calendar.get(java.util.Calendar.DAY_OF_WEEK);
// calendarDay contains the day of the month. It is initialized to
// a negative value and incremented for each cell in the calendar
// so that its value is 1 on the cell for the first day of the month.
int calendarDay = 1 + firstDayOfWeek - firstWeekdayOfMonth;
// what's the last day of the month?
calendar.add(java.util.Calendar.MONTH, 1);
calendar.add(java.util.Calendar.DAY_OF_MONTH, -1);
int lastDayOfMonth =
calendar.get(java.util.Calendar.DAY_OF_MONTH);
int lastWeekOfMonth =
calendar.get(java.util.Calendar.WEEK_OF_MONTH);
// above is identical to JSP
}
}
monthName + " " + calendarYear
monthName + " " + calendarYear
dayNames
dayName
int weekOfMonth = 0; weekOfMonth < lastWeekOfMonth; ++weekOfMonth
int dayOfWeek = 0; dayOfWeek < dayNames.length; ++dayOfWeek, ++calendarDay
calendarDay == thisDay && calendarMonth == thisMonth && calendarYear == thisYear
today
1 <= calendarDay && calendarDay <= lastDayOfMonth
? "this-month"
: "other-month"
1 <= calendarDay && calendarDay <= lastDayOfMonth
? String.valueOf(calendarDay)
: " "
null != (event = (String) dates.get(String.valueOf(calendarMonth+1) + "/" + String.valueOf(calendarDay)))
event