Calendar In Java Example

Chapter: Date and Time Last Updated: 23-09-2016 07:33:10 UTC

Program:

            /* ............... START ............... */
                
import java.util.*;

public class JavaCalendar {
	public static void main(String[] args) {

		Scanner scanner = new Scanner(System.in);
		System.out.print("Enter year: ");
		int year = scanner.nextInt();

		Calendar cal = new GregorianCalendar();

		int startDay;
		int numberOfDays;
		for (int i = 0; i < 12; i++) {
			cal.set(year, i, 1);
			startDay = cal.get(Calendar.DAY_OF_WEEK);
			numberOfDays = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
			System.out.print(cal.getDisplayName(Calendar.MONTH, Calendar.LONG, Locale.US));
			System.out.println(" " + year);
			printMonth(numberOfDays, startDay);
			System.out.println();
		}
	}

	private static void printMonth(int numberOfDays, int startDay) {

		int weekdayIndex = 0;
		System.out.println("Su  Mo  Tu  We  Th  Fr  Sa");

		for (int day = 1; day < startDay; day++) {
			System.out.print("    ");
			weekdayIndex++;
		}

		for (int day = 1; day <= numberOfDays; day++) {
			System.out.printf("%1$2d", day);
			weekdayIndex++;
			if (weekdayIndex == 7) {
				weekdayIndex = 0;
				System.out.println();
			} else {
				System.out.print("  ");
			}
		}
		System.out.println();
	}
}

                /* ............... END ............... */
        

Output

Enter year: 2016
January 2016
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
31  

February 2016
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  

March 2016
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  31  

April 2016
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


May 2016
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  31  

June 2016
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  

July 2016
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
31  

August 2016
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  31  

September 2016
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  

October 2016
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  31  

November 2016
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  

December 2016
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  31


Notes:

  • The Calendar is an abstract class that provides methods for converting between time and calendar fields. Also, the class provides fields and methods for implementing a concrete calendar system.
  • The only implementation of a Calendar that is provided by Java is the GregorianCalendar class. This class provides a standard calendar system that supports both the Julian and Gregorian calendar systems.

Tags

Calendar In Java, Date And Time

Similar Programs Chapter Last Updated
Java Format Time In 24 Hour Format Date and Time 25-10-2016
Java Month In MMM Format Date and Time 25-10-2016
Java Display Date In Different Formats Date and Time 03-09-2016
Java Display Week Number Of The Year Date and Time 03-09-2016
Java Display Time In Italian Language Date and Time 02-09-2016
Java Display Time In Different Country's Format Date and Time 02-09-2016
Java Add Time To Date Example Date and Time 02-09-2016
Java Format Seconds Example Date and Time 02-09-2016
Java Date And Time Display Example Date and Time 02-09-2016
Java Display Hour And Minute Example Date and Time 01-09-2016
Java Displaying Month Name Example Date and Time 01-09-2016
Java Program To Format Date To yyyy/MM/dd Example Date and Time 30-06-2016
Java Program To Format Date To MM/dd/yyyy Example Date and Time 30-06-2016
Java Program To Format Date To dd/MM/yyyy Example Date and Time 30-06-2016
Weekdays In Java Example Date and Time 29-04-2016
Parse String Into Date In Java Date and Time 29-04-2016
Milliseconds To Date In Java Date and Time 29-04-2016
Gregorian Calendar In Java Date and Time 29-04-2016
Add Years To Date In Java Date and Time 29-04-2016
Add Month To Date In Java Date and Time 29-04-2016
Add Hours To Date In Java Date and Time 29-04-2016
SimpleDateFormat In Java Date and Time 28-04-2016
Date Format Using SimpleDateFormat In Java Date and Time 28-04-2016
Date In Java Example Date and Time 28-04-2016
AM PM Time Using SimpleDateFormat In Java Date and Time 28-04-2016
Time Zone In Java Example Date and Time 27-04-2016
Leap Years Between Date Range In Java Date and Time 27-04-2016
Java GMT Time Example Date and Time 27-04-2016
Epoch Time In Java Date and Time 27-04-2016
Day Of Year In Java Date and Time 27-04-2016

1 2