java可视化日历
2017-09-16 本文已影响0人
文艺小年青
可视化日历.png这是Date类的一个小的案例
下边直接上代码:
按照格式输入年月日,会出现对应的日历,并且当前日期会有*标注
// 可视化日历
public class Test02 {
public static void main(String[] args) {
System.out.println("请输入日期-按照格式: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);
//获得老的天数(也就是我们传进来的当天的天数)
int day = calendar.get(Calendar.DATE);
//日期从一号开始
calendar.set(Calendar.DATE, 1);
//获取是周几
//System.out.println(calendar.get(Calendar.DAY_OF_WEEK));
int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK);
//获取一个月最大的天数
//System.out.println(calendar.getActualMaximum(Calendar.DATE));
int maxDate = calendar.getActualMaximum(Calendar.DATE);
System.out.println("日\t一\t二\t三\t四\t五\t六");
for (int i = 1; i < dayOfWeek; i++) {
System.out.print('\t');
}
//计数器
// 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();
}
}
}