Users can customize and print the calendar for a certain year, that is, print out all the calendar for a certain year.
For example, print out the 2013 calendar as follows:
February 2013
----------------------------------------
Sun Mon Tue Wed Thu Fri Sat
2
4 5 6 7 8 9
11 12 13 14 15 16
18 19 20 21 22 23
25 26 27 28
March 2013
----------------------------------------
Sun Mon Tue Wed Thu Fri Sat
2
4 5 6 7 8 9
11 12 13 14 15 16
18 19 20 21 22 23
25 26 27 28 29 30
April 2013
----------------------------------------
Sun Mon Tue Wed Thu Fri Sat
2 3 4 5 6
8 9 10 11 12 13
15 16 17 18 19 20
22 23 24 25 26 27
29 30
May 2013
----------------------------------------
Sun Mon Tue Wed Thu Fri Sat
2 3 4
6 7 8 9 10 11
13 14 15 16 17 18
20 21 22 23 24 25
27 28 29 30 31
Jun 2013
----------------------------------------
Sun Mon Tue Wed Thu Fri Sat
3 4 5 6 7 8
10 11 12 13 14 15
17 18 19 20 21 22
24 25 26 27 28 29
July 2013
----------------------------------------
Sun Mon Tue Wed Thu Fri Sat
2 3 4 5 6
8 9 10 11 12 13
15 16 17 18 19 20
22 23 24 25 26 27
29 30 31
August 2013
----------------------------------------
Sun Mon Tue Wed Thu Fri Sat
twenty three
5 6 7 8 9 10
12 13 14 15 16 17
19 20 21 22 23 24
26 27 28 29 30 31
September 2013
----------------------------------------
Sun Mon Tue Wed Thu Fri Sat
2 3 4 5 6 7
9 10 11 12 13 14
16 17 18 19 20 21
23 24 25 26 27 28
30
October 2013
----------------------------------------
Sun Mon Tue Wed Thu Fri Sat
2 3 4 5
7 8 9 10 11 12
14 15 16 17 18 19
21 22 23 24 25 26
28 29 30 31
November 2013
----------------------------------------
Sun Mon Tue Wed Thu Fri Sat
2
4 5 6 7 8 9
11 12 13 14 15 16
18 19 20 21 22 23
25 26 27 28 29 30
December 2013
----------------------------------------
Sun Mon Tue Wed Thu Fri Sat
2 3 4 5 6 7
9 10 11 12 13 14
16 17 18 19 20 21
23 24 25 26 27 28
30 31
Such as printing: Calendar for January 2014
Today is: 2013-04-27, then the current month’s calendar is printed as follows:
Now enter the code part:
================================================== ======
Code part:
================================================== ======
/UUUUUU_Test/src/com/b510/date/HongtenDate.java
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
/**
* A date processing class. In this class, the constructor <code>HongtenDate()</code> will set the year to the current year by default<br>
* And <code>HongtenDate(int year)</code>, you can customize the year<br>
*
* <pre>
* HongtenDate date = new HongtenDate();
* </pre>
*
* or<br>
*
* <pre>
* HongtenDate hd = new HongtenDate(2014);
* </pre>
*
* Call <code>printCalendar()</code> to print the date of the year<br>
* Call <code>printCurrentMonth()</code> to print the date of the current month<br>
*, of course, you can also call<br>
* <code>printMonthOfYear()</code>Set a parameter to print the date of a certain month<br>
* Here are some reference plans:<br>
*
* <pre>
* // No parameters, the system defaults to the current year
* HongtenDate date = new HongtenDate();
* date.printCalendar();
* date.printCurrentMonth();
* date.printMonthOfYear(4);
* </pre>
*
* or<br>
*
* <pre>
* // Set to 2014
* HongtenDate hd = new HongtenDate(2014);
* hd.printCurrentMonth();
* hd.printMonthOfYear(1);
* </pre>
*
* @date 2013-4-27
* @author hongten
*
*/
public class HongtenDate {
//MONTHS
// ============================================
public static final String JANUARY = "January";
public static final String FEBUARY = "February";
public static final String MARCH = "March";
public static final String APRIL = "April";
public static final String MAY = "May";
public static final String JUN = "Jun";
public static final String JULY = "July";
public static final String AUGUST = "August";
public static final String SEPTERMBER = "September";
public static final String OCTOBER = "October";
public static final String NOVEMBER = "November";
public static final String DECEMBER = "December";
/**
* year
*/
private int year;
/**
* The day of the week on January 1st (eg: 2013-01-01-->Tuesday, so <code>whatDayOnJanuaryOne = 2;</code>)
*/
private int whatDayOnJanuaryOne;
// main
// ======================================
public static void main(String[] args) throws Exception {
// No parameters, the system defaults to the current year
HongtenDate date = new HongtenDate();
//date.printCalendar();
date.printCurrentMonth();
// date.printMonthOfYear(4);
// Set to 2014
HongtenDate hd = new HongtenDate(2014);
// hd.printCurrentMonth();
// hd.printMonthOfYear(1);
}
// No parameters, the system defaults to the current year
public HongtenDate() {
Calendar cal = Calendar.getInstance();
this.year = cal.get(Calendar.YEAR);
try {
setWhatDayOnJanuaryOne(getJanuaryOne(year));
} catch (Exception e) {
e.printStackTrace();
}
}
//With parameters, set the year
public HongtenDate(int year) {
this.year = year;
try {
setWhatDayOnJanuaryOne(getJanuaryOne(year));
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* Print all dates of a certain month
*
* @parammon
* month
* @throwsException
*/
public void printMonthOfYear(int mon) throws Exception {
if (mon < 1 || mon > 12) {
System.out.println("The month you entered [" + mon + "] is wrong, please check in progress....");
return;
}
GregorianCalendar now = new GregorianCalendar();
//Get a Date object
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date date = sdf.parse(year + "-" + mon + "-01");
//Set the current time
now.setTime(date);
// Get the current month from the date
int month = now.get(Calendar.MONTH);
//Set now's date to 1
now.set(Calendar.DAY_OF_MONTH, 1);
// Get the day of the week now is
int week = now.get(Calendar.DAY_OF_WEEK);
//Print calendar header label
System.out.println("日/t一/t二/t三/t四/t五/t六");
//Print the space before the current date
for (int i = Calendar.SUNDAY; i < week; i++) {
System.out.print("/t");
}
//Print the calendar body
while (now.get(Calendar.MONTH) == month) {
int day = now.get(Calendar.DAY_OF_MONTH);
//Align the output calendar, add a space if it is less than 10
if (day < 10) {
System.out.print(" " + day + "/t");
} else {
System.out.print("" + day + "/t");
}
// If it is Saturday, wrap the line
if (week == Calendar.SATURDAY) {
System.out.println();
}
// Each time the date is output, add one day to the date
now.add(Calendar.DAY_OF_MONTH, 1);
// Retrieve the day of the week
week = now.get(Calendar.DAY_OF_WEEK);
}
}
/**
* Print all dates of the current month, this will not change depending on the year set by the user
*/
public void printCurrentMonth() {
GregorianCalendar now = new GregorianCalendar();
//Get a Date object
Date date = new Date();
//Set the current time
now.setTime(date);
//Get the current day from date
int toDay = now.get(Calendar.DAY_OF_MONTH);
// Get the current month from the date
int month = now.get(Calendar.MONTH);
//Set now's date to 1
now.set(Calendar.DAY_OF_MONTH, 1);
// Get the day of the week now is
int week = now.get(Calendar.DAY_OF_WEEK);
//Print calendar header label
System.out.println("日/t一/t二/t三/t四/t五/t六");
//Print the space before the current date
for (int i = Calendar.SUNDAY; i < week; i++) {
System.out.print("/t");
}
//Print the calendar body
while (now.get(Calendar.MONTH) == month) {
int day = now.get(Calendar.DAY_OF_MONTH);
//Align the output calendar, add a space if it is less than 10
if (day < 10) {
// If it is the current date, add a label
if (day == toDay) {
System.out.print(":)" + day + "(:/t");
} else {
System.out.print(" " + day + "/t");
}
} else {
// If it is the current date, add a label
if (day == toDay) {
System.out.print(":)" + day + "(:/t");
} else {
System.out.print("" + day + "/t");
}
}
// If it is Saturday, wrap the line
if (week == Calendar.SATURDAY) {
System.out.println();
}
// Each time the date is output, add one day to the date
now.add(Calendar.DAY_OF_MONTH, 1);
// Retrieve the day of the week
week = now.get(Calendar.DAY_OF_WEEK);
}
}
/**
* Get the day of the week that January 1st of this year is
*
* @param year
* year
* @return
* @throwsException
*/
public int getJanuaryOne(int year) throws Exception {
int[] weekDays = { 0, 1, 2, 3, 4, 5, 6 };
Calendar cal = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date dt = sdf.parse(year + "-01-01");
cal.setTime(dt);
int w = cal.get(Calendar.DAY_OF_WEEK) - 1;
if (w < 0)
w = 0;
return weekDays[w];
}
/**
* Print all months of the year
*/
public void printCalendar() {
for (int i = 1; i <= 12; i++) {
String month = getMonth(i);
printTitle(month);
//Print months with 31 days
if (i == 1 || i == 3 || i == 5 || i == 7 || i == 8 || i == 10 || i == 12) {
print31();
}
//Print the month with 30 days
else if (i == 4 || i == 6 || i == 9 || i == 11) {
print30();
}
// print February
else if (i == 2) {
printFebuary();
}
System.out.println();
}
}
//Print format
// ============================================ start
/**
* Print February. February may be different in each year, so it must be processed separately.
*/
protected void printFebuary() {
if (this.year % 4 == 0) {
// leap year
printLeapYear();
} else {
// normal year
printNonleapYear();
}
}
/**
*Print format for February in leap year
*/
private void printLeapYear() {
for (int j = 1; j <= 29; j++) {
String tmp = "";
if (j == 1) {
for (int k = 1; k <= this.whatDayOnJanuaryOne % 7; k++) {
tmp = tmp + " ";
}
tmp = tmp + " " + j + " ";
if (this.whatDayOnJanuaryOne == 6) {
System.out.println(tmp);
} else {
System.out.print(tmp);
}
} else if (j > 1 && j < 29) {
if ((this.whatDayOnJanuaryOne + j) % 7 == 0) {
System.out.println(" " + j);
} else {
if (j < 10) {
System.out.print(" " + j + " ");
} else {
System.out.print(" " + j + " ");
}
}
} else if (j == 29) {
System.out.println(" " + j);
this.whatDayOnJanuaryOne = ((this.whatDayOnJanuaryOne + j) % 7);
}
}
}
/**
* Print the format of February in ordinary years
*/
private void printNonleapYear() {
for (int j = 1; j <= 28; j++) {
String tmp = "";
if (j == 1) {
for (int k = 1; k <= this.whatDayOnJanuaryOne % 7; k++) {
tmp = tmp + " ";
}
tmp = tmp + " " + j + " ";
if (this.whatDayOnJanuaryOne == 6) {
System.out.println(tmp);
} else {
System.out.print(tmp);
}
} else if (j > 1 && j < 28) {
if ((this.whatDayOnJanuaryOne + j) % 7 == 0) {
System.out.println(" " + j);
} else {
if (j < 10) {
System.out.print(" " + j + " ");
} else {
System.out.print(" " + j + " ");
}
}
} else if (j == 28) {
System.out.println(" " + j);
this.whatDayOnJanuaryOne = ((this.whatDayOnJanuaryOne + j) % 7);
}
}
}
/**
* Print months with 30 days
*/
protected void print30() {
for (int j = 1; j <= 30; j++) {
String tmp = "";
if (j == 1) {
for (int k = 1; k <= this.whatDayOnJanuaryOne % 7; k++) {
tmp = tmp + " ";
}
tmp = tmp + " " + j + " ";
if (this.whatDayOnJanuaryOne == 6) {
System.out.println(tmp);
} else {
System.out.print(tmp);
}
} else if (j > 1 && j < 30) {
if ((this.whatDayOnJanuaryOne + j) % 7 == 0) {
System.out.println(" " + j);
} else {
if (j < 10) {
System.out.print(" " + j + " ");
} else {
System.out.print(" " + j + " ");
}
}
} else if (j == 30) {
System.out.println(" " + j);
this.whatDayOnJanuaryOne = ((this.whatDayOnJanuaryOne + j) % 7);
}
}
}
/**
* Print months with 31 days
*/
protected void print31() {
for (int j = 1; j <= 31; j++) {
String tmp = "";
if (j == 1) {
for (int k = 1; k <= this.whatDayOnJanuaryOne % 7; k++) {
tmp = tmp + " ";
}
tmp = tmp + " " + j + " ";
if (this.whatDayOnJanuaryOne == 6) {
System.out.println(tmp);
} else {
System.out.print(tmp);
}
} else if (j > 1 && j < 31) {
if ((this.whatDayOnJanuaryOne + j) % 7 == 0) {
System.out.println(" " + j);
} else {
if (j < 10) {
System.out.print(" " + j + " ");
} else {
System.out.print(" " + j + " ");
}
}
} else if (j == 31) {
System.out.println(" " + j);
this.whatDayOnJanuaryOne = ((this.whatDayOnJanuaryOne + j) % 7);
}
}
}
/**
*Print the title of each month
*
* @param month
*/
protected void printTitle(String month) {
System.out.println(" " + month + " " + this.year + " ");
System.out.println("--------------------------------------------- --");
System.out.println("Sun Mon Tue Wed Thu Fri Sat");
}
//Print format
// ============================================== end
/**
* Get the English name of the month
*
* @param m
* Numerical representation of the month
* @return
*/
public String getMonth(int m) {
String month = "";
switch (m) {
case 1:
month = JANUARY;
break;
case 2:
month = FEBUARY;
break;
case 3:
month = MARCH;
break;
case 4:
month = APRIL;
break;
case 5:
month = MAY;
break;
case 6:
month = JUN;
break;
case 7:
month = JULY;
break;
case 8:
month = AUGUST;
break;
case 9:
month = SEPTERMBER;
break;
case 10:
month = OCTOBER;
break;
case 11:
month = NOVEMBER;
break;
case 12:
month = DECEMBER;
break;
}
return month;
}
public int getYear() {
return year;
}
public void setYear(int year) {
this.year = year;
}
public int getWhatDayOnJanuaryOne() {
return whatDayOnJanuaryOne;
}
public void setWhatDayOnJanuaryOne(int whatDayOnJanuaryOne) {
this.whatDayOnJanuaryOne = whatDayOnJanuaryOne;
}
}