练习小程序_可视化日历
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Scanner;
public class TestCalendar throws ParseException{
public static void main(String[] args) { System.out.println("请按照格式输入日期:年-月-日");
Scanner scanner =new Scanner(System.in);
String str=scanner.nextLine();
SimpleDateFormat df=new SimpleDateFormat("yyyy-MM-dd");
Date date=df.parse(str);
Calendar c=new GregorianCalendar();
c.setTime(date);
System.out.println("日\t一\t二\t三\t四\t五\t六"); c.set(Calendar.DATE, 1);
int firstDay=c.get(Calendar.DAY_OF_WEEK)-1;
for(int i=0;i<firstDay;i++) {
System.out.print("\t");
}
for(int i=1;i<=days;i++) {
System.out.print(i+"\t");
int nextLine=c.get(Calendar.DAY_OF_WEEK)-1; if(nextLine==Calendar.SATURDAY-1) { System.out.print('\n');
}
c.add(Calendar.DATE,1);
}
}
}
![在这里插入图片描述](https://img-blog.csdnimg.cn/20200415223412920.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80NTg0MDUxOQ==,size_16,color_FFFFFF,t_70)