JAVA -- output calendar of specified month and year
1. purpose
Output a calendar of one month of the current time in the current time zone (1 to 28 / 29 / 30 / 31, and the day of the week for each day)
2. grammar
GregorianCalendar(): constructs a new object using machine time.
Gregorian calendar (int year, int month, int dayofmonth, int houseofday, int minute, int second): constructs a new object with the specified year, month, day, hour, minute, and second.
Gregorian calendar (locale a locale): constructs a new object with the locale, default time zone, and local time specified by a locale.
Gregorian calendar (TimeZone zone): constructs a new object with the time zone specified by zone, the default zone, and the current time.
Gregorian calendar (timezone zone, local alocal): constructs a new object with the time zone specified by zone, the region specified by alocal, and the current time.
3. code
import java.util.Calendar;
import java.util.GregorianCalendar;
//import java.util.Scanner;
public class rw9 {
static final String week[]= {"Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"};
public static void main(String[] args) {
// TODO Auto-generated method stub
int i;
int year=2018;//Set the specified year
int month=9;//Set the specified month
System.out.println(year + "year"+ month + "The calendar of the month is as follows:\n");
System.out.println("=========================================");
GregorianCalendar cal=new GregorianCalendar(year,month-1,1);//Create objects for year, month, and first day
int totalDays =cal.getActualMaximum(Calendar.DAY_OF_MONTH);//Get the days of the month
int startDay =cal.get(Calendar.DAY_OF_WEEK)-1;//What day of the week is the first day of the month
for(i=0;i<week.length;i++)
System.out.print(week[i]+" ");//Output 7 days of week
System.out.println();
for(i=0;i<startDay;i++)
System.out.print(" ");//Output spaces before the first day
for(int day =1;day<=totalDays;day++) {
if(day<=8) { //For output control
System.out.print(day + " ");//Output each day in turn
}else {
System.out.print(day+" ");
}
i++;
if(i==7) { //Line break after losing every week
System.out.println();
i=0;
}
}
}
}
4. results
The compilation results are as follows:
The operation results are as follows: