package Testdate;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Scanner;
/**
* 可视化日历练习
* @author Administrator
*
*/
public class Test {
public static void main(String[] args) {
System.out.println("请输入一个日期(格式:yyyy-MM-dd):");
String shuru = new Scanner(System.in).nextLine();
String time = shuru;//"2018-8-8";
SimpleDateFormat dateF = new SimpleDateFormat("yyyy-MM-dd");
try {
Date date = dateF.parse(time);
GregorianCalendar Calendar = new GregorianCalendar();
Calendar.setTime(date);
int day = Calendar.get(Calendar.DATE);
Calendar.set(Calendar.DATE, 1);
//获得本月1号是周几
//System.out.println();
int dateweek = Calendar.get(Calendar.DAY_OF_WEEK);
//获得本月最后一天
//System.out.println();
int maxdate = Calendar.getActualMaximum(Calendar.DATE);
System.out.println("日 一 二 三 四 五 六");
for(int i=1;i<dateweek;i++){
System.out.print(" ");
}
// int j = 0;
for(int i = 1; i <= maxdate; i++){
if (i == day) {
System.out.print("*");
}
System.out.print(i+" ");
// j++;
// if(j % 7 == 0){
int dateweek2 = Calendar.get(Calendar.DAY_OF_WEEK);
if(dateweek2 == Calendar.SATURDAY){
System.out.println();
}
Calendar.add(Calendar.DATE, 1); //每打印一次日期+1
}
} catch (ParseException e) {
e.printStackTrace();
}
}
}
最终控制台显示: