代码实现:
package demo0520;
/**
*@author deep-learning
*@date 2018-5-20
*/
import org.junit.Test;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
public class demo4 {
@Test
public void printCalendar() {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
System.out.println(sdf.format(new Date()));
int month[] = {31,28,31,40,31,30,31,31,30,31,30,31};
Calendar calendar = Calendar.getInstance();
int cday = calendar.get(Calendar.DATE);
calendar.set(Calendar.DATE,1);
int cweek = calendar.get(Calendar.DAY_OF_WEEK);
int cmonth = calendar.get(Calendar.MONTH);
int cyear = calendar.get(Calendar.YEAR);
if (cyear % 4 == 0 && cyear % 100 != 0 || cyear % 400 == 0)
month[1] = 29;
String[] week = {"日", "一", "二", "三", "四", "五", "六"};
for (String v : week) {
System.out.print(v + "\t");
}
System.out.println();
for (int i = 0;i < 26;i++)
System.out.print("-");
System.out.println();
for (int i = 0;i < cweek - 1;i++)
System.out.print("\t");
for (int i = 1;i <= month[cmonth];i++){
System.out.print(i+"\t");
if ((i + cweek - 1)%7 == 0)
System.out.println();
}
}
}
运行截图: