Recipe 6.11 Creating a Calendar Page


Problem

You want a calendar for a given month of a given year, or of the current month and year.

Solution

Use Calendar.get( ) to find what day of the week the first of the month falls on and format accordingly.

Discussion

Like the output of the Unix cal command, it is often convenient to view a month in compact form. The basic idea is to find what day of the week the first of the month is and print blank columns for the days of the week before the month begins. Then, print the numbers from 1 to the end of the month, starting a new row after you get to the last day of each week.

Here's my program, compared to the Unix cal command:

daroad.darwinsys.com$ java CalendarPage 6 2000 June 2000 Su Mo Tu We Th Fr Sa              1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30  daroad.darwinsys.com$ cal 6 2000      June 2000 Su Mo Tu We Th Fr Sa              1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30

The source code is simple and straightforward (Example 6-3).

Example 6-3. CalendarPage.java
import java.util.*; import java.text.*; /** Print a month page.  * Only works for the Western calendar.   */ public class CalendarPage {     /** The names of the months */     String[] months = {         "January", "February", "March", "April",         "May", "June", "July", "August",         "September", "October", "November", "December"     };     /** The days in each month. */     public final static int[] dom = {             31, 28, 31, 30,    /* jan feb mar apr */             31, 30, 31, 31, /* may jun jul aug */             30, 31, 30, 31    /* sep oct nov dec */     };     /** Compute which days to put where, in the Cal panel */     public void print(int mm, int yy) {         /** The number of days to leave blank at the start of this month */         int leadGap = 0;         System.out.print(months[mm]);        // print month and year         System.out.print(" ");         System.out.print(yy);         System.out.println( );         if (mm < 0 || mm > 11)             throw new IllegalArgumentException("Month " + mm + " bad, must be 0-11");         GregorianCalendar calendar = new GregorianCalendar(yy, mm, 1);         System.out.println("Su Mo Tu We Th Fr Sa");         // Compute how much to leave before the first.         // get(DAY_OF_WEEK  ) returns 0 for Sunday, which is just right.         leadGap = calendar.get(Calendar.DAY_OF_WEEK)-1;         int daysInMonth = dom[mm];         if (calendar.isLeapYear(calendar.get(Calendar.YEAR)) && mm == 1)             ++daysInMonth;         // Blank out the labels before 1st day of month         for (int i = 0; i < leadGap; i++) {             System.out.print("   ");         }         // Fill in numbers for the day of month.         for (int i = 1; i <= daysInMonth; i++) {             // This "if" statement is simpler than fiddling with NumberFormat             if (i<=9)                 System.out.print(' ');             System.out.print(i);             if ((leadGap + i) % 7 == 0)        // wrap if end of line.                 System.out.println( );             else                 System.out.print(' ');         }         System.out.println( );     }     /** For testing, a main program */     public static void main(String[] av) {         int month, year;         CalendarPage cp = new CalendarPage( );         // print the current month.         if (av.length == 2) {             cp.print(Integer.parseInt(av[0])-1, Integer.parseInt(av[1]));         } else {             Calendar c = Calendar.getInstance( );             cp.print(c.get(Calendar.MONTH), c.get(Calendar.YEAR));         }     } }



Java Cookbook
Java Cookbook, Second Edition
ISBN: 0596007019
EAN: 2147483647
Year: 2003
Pages: 409
Authors: Ian F Darwin

Similar book on Amazon

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net