使用calendar实现日历:
public class Calen {
public static void main(String[] args) throws ParseException {
Scanner in = new Scanner(System.in);
System.out.println("请输入时间");
String str = in.nextLine();
//String str = "2012-12-1";
DateFormat dFormat = new SimpleDateFormat("yyyy-MM-dd");
Date date = dFormat.parse(str);
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
int day = calendar.get(Calendar.DATE);
//System.out.printf("%s\t","日一二三四五六");
System.out.print("日\t一\t二\t三\t四\t五\t六");
System.out.println();
calendar.set(Calendar.DATE, 1);
int week = calendar.get(Calendar.DAY_OF_WEEK);
for(int i = 1;i<week;i++){
System.out.print("\t");
}
int max = calendar.getActualMaximum(Calendar.DATE);
for(int i = 1;i<=max;i++){
if(i==day){
System.out.print("*");
}
System.out.print(i+"\t");
week = calendar.get(Calendar.DAY_OF_WEEK);
if(week==7){
if(week==Calendar.SATURDAY){
System.out.println();
//calendar.add(Calendar.DATE, 1);
}
//System.out.println();
}
calendar.add(Calendar.DATE, 1);
}
}
}
这里主要是calendar类的使用方法。所有都是通过calendar类中的方法实现的