This is a little system of perpetual calendar written with java. There maybe some bugs in it, I’ll be appreciate it if you can find and tell me about them. Here goes codes:
package work;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import javax.swing.text.SimpleAttributeSet;
public class calendar {
public static void main(String[] args) throws ParseException {
String s; //输入的字符串
Scanner S = new Scanner(System.in);
System.out.print("请输入时间(格式:2020-2-20):");
s = S.next();
s=s.trim();
String t[] = s.split("-");
String t2[] = new String[3];
for (int i=0;i<3;i++) {
if (t[i].length()==1){
t2[i] =(String)("0"+t[i]);
}
else {
t2[i]=t[i];
}
}
int Mark = Integer.parseInt(t2[2]);
while (!Judge(t2)) {
System.out.println("输入的数据有误,请再输入一次:");
s = S.next();
s=s.trim();
t = s.split("-");
t2 = new String[3];
for (int i=0;i<3;i++) {
if (t[i].length()==1){
t2[i] =(String)("0"+t[i]);
}
else {
t2[i]=t[i];
}
}
Mark = Integer.parseInt(t2[2]);
}
System.out.println("您刚输入的日期是:"+t2[0]+"年"+t2[1]+"月"+t2[2]+"日");
DateFormat b = new SimpleDateFormat("yyyy-MM-dd");
String j = t2[0]+"-"+t2[1]+"-"+t2[2];
Date c = b.parse(j);
System.out.println("日\t一\t二\t三\t四\t五\t六");
Calendar d = new GregorianCalendar();
d.setTime(c);
d.set(Calendar.DAY_OF_MONTH,1);
int record = d.getActualMaximum(Calendar.DATE);
for (int i=0;i<d.get(Calendar.DAY_OF_WEEK)-1;i++) {
System.out.print("\t");
}
for (int i=0;i<record;i++){
if (i+1!=Mark) {
System.out.print(d.get(Calendar.DAY_OF_MONTH)+"\t");
}
else {
System.out.print(d.get(Calendar.DAY_OF_MONTH)+"*"+" ");
}
if (d.get(Calendar.DAY_OF_WEEK)==7){
System.out.println();
}
d.add(Calendar.DAY_OF_MONTH,1);
}
}
public static boolean Judge(String[] a){
int b[] = new int[3];
for (int i=0;i<3;i++){ //Convert the String contents into int
b[i] = Integer.parseInt(a[i]); //Autounboxing
}
if (b[0]%400==0||b[0]%4==0&&b[0]%100!=1){ //闰年
if (b[2]<=28) {
return true;
}
if (b[2]==29){
return (b[1]==2);
}
else if(b[2]==30){
return (b[1]==4||b[1]==6||b[1]==9||b[1]==11);
}
else if(b[2]==31){
return (b[1]==1||b[1]==3||b[1]==5||b[1]==7||b[1]==8||b[1]==10||b[1]==12);
}
return false;
}
else {
if (b[2]<=28) {
return true;
}
if (b[2]==28){
return (b[1]==2);
}
else if(b[2]==30){
return (b[1]==4||b[1]==6||b[1]==9||b[1]==11);
}
else if(b[2]==31){
return (b[1]==1||b[1]==3||b[1]==5||b[1]==7||b[1]==8||b[1]==10||b[1]==12);
}
return false;
}
}
}