Core Java| JSP| Servlets| XML| EJB| JEE5| Web Services| J2ME| Glossary| Questions?

 

 

 

 

 

 

 

 

 

 

 

 

 

Search Tutorials

Latest Questions
Comments
 
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.

 

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.

                         

» View all related tutorials
Related Tags: c file array class list ui lists method get name using this oo root example where to exam drive store

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

Audio Version
Reload Image
 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Training Courses
Tell A Friend
Your Friend Name
Software Solutions
Least Viewed
Most Rated
Recently Viewed
Search Tutorials

 

 
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Indian Software Development Company | iPhone Development Company in India | Flex Development Company in India | Java Training Delhi | Java Training at Noida |

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2008. All rights reserved.