Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Java Beginners Calendar

 
 

Share on Google+Share on Google+

Calendar

Advertisement
In this section we are displaying the days of a specific month and the calendar format of the days for this month. We are also checking whether the year is leap or not.

Calendar

     

In this section we are displaying the days of  a specific month and the calendar format of the days for this month. We are also checking whether the year is leap or not.

Description of program: 

In the given example first we are taking a string array monthcalendar that contains the name of the months of an year. After that we are creating an integer type array that contains number of days in the months in their respective order. Now we create a method displayMonth(). In this method we are leaving the number of days blank at the start of the month, throwing an illegalArgumentException object showing a message if the month value does not lie in between 0 and 11. Now we are creating an object cldr of the Gregoriancalendar class by passing the values of month and year and then printing the days of the week on the console. Now we are getting the number of days to be left blank at the start of a month by using the get() method of the Gregoriancalendar class and also checking whether the year is leap or not and printing the days of that month on the console.

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

public class MonthCalender {
  
  public final static String[] monthcalender = {
  "January""February""March""April""May""June""July",
  "August""September""October""November""December"};
  
  public final static int daysinmonths[] {3128313031
    30
313130313031 };
  
  private void displayMonth(int month, int year) {
  
  // The number of days to leave blank at
  // the start of this month.
  
  int blankdays = 0;
  System.out.println("  " + monthcalender[month" " + year);
  
  if (month < || month > 11) {
  throw new IllegalArgumentException(
  "Month " + month + " is not valid and must lie in 

between 0 and 11");
  }
  
  GregorianCalendar cldr = new GregorianCalendar(year, 

month, 1);
  System.out.println("Sunday Monday Tuesday Wednesday 

Thirsday Friday Saturday");
  
  // Compute how much to leave before before the first

day of the month.
  // getDay() returns 0 for Sunday.
  
  blankdays = cldr.get(Calendar.DAY_OF_WEEK)-1;
  int daysInMonth = daysinmonths[month];
  
  if (cldr.isLeapYear(cldr.get(Calendar.YEAR)) 

&& month == 1) {
  
  ++daysInMonth;
  }
  
  // Blank out the labels before 1st day of the month
  for (int i = 0; i < blankdays; i++) {
  System.out.print(" ");
  }
  
  for (int i = 1; i <= daysInMonth; i++) {
  
  // This "if" statement is simpler than 

messing with NumberFormat
  if (i<=9) {
  System.out.print(" ");
  }
  System.out.print(i);

  if ((blankdays + i== 0) { // Wrap if EOL
  System.out.println();
  }
  else {
  System.out.print(" ");
  }
  }
  }
  
  /**
 * Sole entry point to the class and application.
 @param args Array of String arguments.
 */
  public static void main(String[] args) {
  
  int mon, yr;
  MonthCalender moncldr = new MonthCalender();

  if (args.length == 2) {
  moncldr.displayMonth(Integer.parseInt(args[0])-1,

 Integer.parseInt(args[1]));
  }
  else {
  Calendar todaycldr = Calendar.getInstance();
  moncldr.displayMonth(todaycldr.get(Calendar.MONTH)

, todaycldr.get(Calendar.YEAR));
  }
  }
}

Here is the output:

C:\Examples>java MonthCalender
December 2007
Sunday Monday Tuesday Wednesday Thursday Friday Saturday
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 31


Download of this  program.

Advertisement

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: December 19, 2008

Related Tutorials

Discuss: Calendar   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:1
beerendra
November 6, 2011
How to compile

Compile your program with javac
DMCA.com