這是Date類的一個(gè)小的案例
下邊直接上代碼:
按照格式輸入年月日,會(huì)出現(xiàn)對(duì)應(yīng)的日歷,并且當(dāng)前日期會(huì)有*標(biāo)注
// 可視化日歷
public class Test02 {
public static void main(String[] args) {
System.out.println("請(qǐng)輸入日期-按照格式:2012-2-12");
Scanner scanner = new Scanner(System.in);
String temp = scanner.nextLine();
DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
//異常捕獲
try {
Date date = format.parse(temp);
Calendar calendar = new GregorianCalendar();
calendar.setTime(date);
//獲得老的天數(shù)(也就是我們傳進(jìn)來(lái)的當(dāng)天的天數(shù))
int day = calendar.get(Calendar.DATE);
//日期從一號(hào)開始
calendar.set(Calendar.DATE, 1);
//獲取是周幾
//System.out.println(calendar.get(Calendar.DAY_OF_WEEK));
int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK);
//獲取一個(gè)月最大的天數(shù)
//System.out.println(calendar.getActualMaximum(Calendar.DATE));
int maxDate = calendar.getActualMaximum(Calendar.DATE);
System.out.println("日\(chéng)t一\t二\t三\t四\t五\t六");
for (int i = 1; i < dayOfWeek; i++) {
System.out.print('\t');
}
//計(jì)數(shù)器
// int j = 0;
for (int i = 1; i < maxDate; i++) {
if (i == day) {
System.out.print("*");
}
System.out.print(i+"\t");
int w = calendar.get(Calendar.DAY_OF_WEEK);
if (w == Calendar.SATURDAY) {
System.out.println();
}
calendar.add(Calendar.DATE, 1);
}
} catch (ParseException e) {
e.printStackTrace();
}
}
}