Java implementation of perpetual calendar

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;
		}
	}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值