JAVA使用类的方法编写一个日历,可以显示当前日期和特殊日期(指定日期)

定义一个test类`

import java.util.Scanner;
public class test{
	int Year;
	int Mouth;
	int Day;
	

	test(int Year,int Mouth,int Day)
   {
	   this.Year=Year;
	   this.Mouth=Mouth;
	   this.Day=Day;  
   }
   
   int GetYear() {
		return Year;
	}
	void SetYear(int Year) {
		this.Year=Year;	
	}
	
	int Getmouth() {
		return Mouth;
	}
	void Setmouth(int Mouth) {
		this.Mouth=Mouth;
	}
	int Getday() {
		return Day;
	}
	void Setday(int Day) {
		this.Day=Day;	
   }
     void show(){ 
 
//计算总共的天数,下边需要计算一共度过了多少个星期以求出某月一号星期几
            int sum=0;
            for(int year=1900;year<Year;year++){
                if(Year%4==0&&Year%100!=0||Year%400==0){
                    sum+=366;
                }else{
                    sum+=365;
                }
            }
            for(int month=1;month<Mouth;month++){
                if(month==2){
                    if(Year%4==0&&Year%100!=0||Year%400==0){
                        sum+=29;
                    }else{
                        sum+=28;
                    }
                }else{
                    if(month==4||month==6||month==9||month==11){
                        sum+=30;
                    }else{
                        sum+=31;
                    }
                }
            }
//表头及第一行空格
            sum+=1;
            int wekday=sum%7;
            System.out.println("星期日\t星期一\t星期二\t星期三\t星期四\t星期五\t星期六");
            for(int i=0;i<=wekday;i++){
                System.out.print("\t");
            }
//表头以下部分            
            int j;
            if(Year%4==0&&Year%100!=0||Year%400==0){
                if(Mouth==2){
                    j=29;
                }else{
                    if(Mouth==4||Mouth==6||Mouth==9||Mouth==11){
                        j=30;
                    }else{
                        j=31;
                    }
                }
            }else{
                if(Mouth==2){
                    j=28;
                }else{
                    if(Mouth==4||Mouth==6||Mouth==9||Mouth==11){
                        j=30;
                    }else{
                        j=31;
                    }
                }
            }
            for(int i=1;i<=j;i++){
            	  if(i==Day) {
          	   System.out.print("*");}
          	  
                if(sum%7==6){
                    System.out.print("\n"+i+"\t");
                  
                }else{
                    System.out.print(i+"\t");
                }
                sum++;
            }
            System.out.println();
           
    }
}

在定义一个test1编写主函数,调用test

import java.text.SimpleDateFormat;
	import java.util.Date;
import java.util.Scanner;

public class test1 {
		
		public static void main(String[] args){ 
			
			//读取当前日期
				
				Date now = new Date(); 
				SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd/HH:mm:ss");//日期格式  
				String hehe = dateFormat.format( now ); 
				System.out.println(hehe); 
				String[] temp=hehe.split("/");
				int Year=Integer.parseInt(temp[0]);
				int Month=Integer.parseInt(temp[1]);
				int Day=Integer.parseInt(temp[2]); 
	          //调用并赋值
				test Date1=new test(2019,6,6);
			/*	Date1.Setmouth(6);System.out.println(Date1.Getmouth()+"月日历");
				Date1.Setday(6);System.out.println(Date1.Getday());*/
				Date1.show();
				
	}	
	}

该程序只是实现了简单的功能,并不具备类似小程序一样所具有的人性化界面,抱歉。

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值