/**
* Created by GETTING MORE on 2017/6/7.
*/
import java.text.DateFormatSymbols;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.Locale;
public class Date {
public static void main(String[] args) {
Locale.setDefault(Locale.CHINA);//此行可加也可不加;
GregorianCalendar d = new GregorianCalendar();
int today = d.get(Calendar.DAY_OF_MONTH);
int month = d.get(Calendar.MONTH); //month的取值从0开始,即比正常值少一,如6月,实际输出为5
// int week=d.get(Calendar.DAY_OF_WEEK);
//System.out.print(week);
d.set(Calendar.DAY_OF_MONTH, 1);
//set today is the first day of this month
// int Da=d.get(Calendar.DAY_OF_MONTH);
// System.out.print(Da);
int weekday = d.get(Calendar.DAY_OF_WEEK); //weekday is a number of 1-7,the monday is 2;and the sunday is 1.
//System.out.print(weekday);
int firstDayOfWeek = d.getFirstDayOfWeek();
//System.out.print(firstDayOfWeek);
int indent = 0;
while (weekday != firstDayOfWeek) {
indent++;
d.add(Calendar.DAY_OF_MONTH, -1); //这里起始值是1(因为上面已经把今天设置为本月第一天)
// 所以减1后调到上个月最后一天)
//int nowday=d.get(Calendar.DAY_OF_MONTH);
// System.out.print(nowday);
weekday = d.get(Calendar.DAY_OF_WEEK);
//System.out.print(weekday);
}
//System.out.print(indent);
//System.out.print("#"+weekday+"#");
//方法1: String[] weekdayNames = {"","Sun","Mon","Tue","Thu","Wed","Fri","Sat"};
String[] weekdayNames=new DateFormatSymbols().getShortWeekdays(); //方法2
do {
System.out.printf("%4s", weekdayNames[weekday]);
d.add(Calendar.DAY_OF_MONTH, 1);//这里起始值为上月的28号
weekday = d.get(Calendar.DAY_OF_WEEK);
}
while (weekday != firstDayOfWeek);
System.out.println();
for (int i = 1; i <= indent; i++)
System.out.print(" ");
d.add(Calendar.DAY_OF_MONTH, 1); //这里用DAY_OF_MONTH或DAY_OF_WEEK输出结果一致;
d.set(Calendar.DAY_OF_MONTH,1);
do {
int day = d.get(Calendar.DAY_OF_MONTH);
System.out.printf("%4d", day);
if (day == today)System.out.print("* ");
else System.out.print(" ");
d.add(Calendar.DAY_OF_MONTH, 1);
weekday = d.get(Calendar.DAY_OF_WEEK);
if (weekday == firstDayOfWeek)
System.out.println();
}
while (d.get(Calendar.MONTH)== month);//因为这里的错误,耽误了大把的时光。错误原因:检查不认真,把Calendar.Month
//写成了Calendar.Day_Of_Month,造成出错。
if (weekday!=firstDayOfWeek)
System.out.println();
}
}