Easy to Learn Java: Programming Articles, Examples and Tips

Start with Java in a few days with Java Lessons or Lectures

Home

Code Examples

Java Tools

More Java Tools!

Java Forum

All Java Tips

Books

Submit News
Search the site here...
Search...
 

Print a month page. Only works for the Western calendar. Java code example - Click here to copy ->>>

   Can't find what you're looking for? Try our search:

Really working examples categorized by API, package, class. You can compile and run our examples right away! Not from source code for Java projects - only working examples! Copy, compile and run!
------------------

Code:

 
/*
 * Copyright (c) Ian F. Darwin, http://www.darwinsys.com/, 1996-2002.
 * All rights reserved. Software written by Ian F. Darwin and others.
 * $Id: LICENSE,v 1.8 2004/02/09 03:33:38 ian Exp $
*/


import java.util.*;
import java.text.*;

/** Print a month page.
 * Only works for the Western calendar.
 * @author   Ian F. Darwin, http://www.darwinsys.com/
 * @version $Id: CalendarPage.java,v 1.7 2004/03/07 03:19:41 ian Exp $
 */
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));
      }
   }
}

 
 



References.

The list of classes which were used on this page you can find below. The links to Java API contain official SUN documentation about all used classes.

  java.util.GregorianCalendar

  java.lang.IllegalArgumentException

  java.text.NumberFormat

  java.util.Calendar




[ Go Back ]



Home Code Examples Java Forum All Java Tips Books Submit News, Code... Search... Offshore Software Tech Doodling

RSS feed Java FAQ RSS feed Java FAQ News     

    RSS feed Java Forums RSS feed Java Forums

All logos and trademarks in this site are property of their respective owner. The comments are property of their posters, all the rest 1999-2006 by Java FAQs Daily Tips.

Interactive software released under GNU GPL, Code Credits, Privacy Policy