//import ohjava.*; import java.util.*; public class PrintCalendar { public static void main(String args[]) { final String monthNames = "JanFebMarAprMayJunJulAugSepOctNovDec"; GregorianCalendar today; String monthString; int dayOfWeek, lastDay, month, year; today = new GregorianCalendar(); today.set(Calendar.DATE,1); month = today.get(Calendar.MONTH) + 1; year = today.get(Calendar.YEAR); dayOfWeek = today.get(Calendar.DAY_OF_WEEK) - 1; monthString = monthNames.substring((month-1)*3,month*3); System.out.println("\n " + monthString + " " + year); System.out.println(" S M T W T F S"); System.out.println("--------------------"); for (int i = 0; i < dayOfWeek; i++) System.out.print(" "); lastDay = daysInMonth(month,year); for (int day=1; day <= lastDay; day++) { if (day <= 9) System.out.print(" " + day + " "); else System.out.print(day + " "); if ((dayOfWeek+day)%7 == 0) System.out.println(); } System.out.println(); } public static int daysInMonth(int month, int year) { int numberOfDays = 31; switch (month) { case 2: numberOfDays = 28; if (year%4 == 0) { numberOfDays = 29; if (year%100 == 0 && year%400 != 0) numberOfDays = 28; } break; case 4: case 6: case 9: case 11: numberOfDays = 30; break; } return numberOfDays; } }