java日历显示年份、月份

import java.util.Scanner;

class CalendarMain
{
    //主函数入口
    public static void main(String[] args)
    {
        System.out.println("*****请输入日期(格式如: 2011-01)*****");
        Scanner sc = new Scanner(System.in);
        String ym = sc.next();
        //截取字符串
        String[] yearMonth = ym.split("-");
        //将字符串转化成整型
        int year = Integer.parseInt(yearMonth [0]);
        int month = Integer.parseInt(yearMonth [1]);
        Year.fun(year);
        Month.fun(month,year);
    }
}

//获取闰年个数
class Year
{
    //多少个闰年
    static int leapYear = 0;

    public static void fun(int year){
        if(year > 1990 && year <10000){
            while(year >= 1990){
                if(year % 4 == 0 && year % 100 != 0 || (year % 400 ==0 )){
                    leapYear ++;
                }
                year --;
            }
        }else{
            System.out.println("请输入查询的年份大于或等于1990年!");
        }
    }
}

//获取一个月多少天和总天数
class Month
{
    public static void fun(int month,int year){
        int monthDay = 0;
        Year y = new Year();
        int days = y.leapYear + (year - 1990)*365;
        if(month > 0 && month <=12){
            for(int i = 1;i <= month;i++){
                if(i < 8 && i != 2){
                    days += i % 2 == 1 ? 31 :30;
                    monthDay = i % 2 == 1 ? 31 :30;
                }
                if(i > 7){
                    days += i % 2 == 1 ? 30 :31;
                    monthDay  = i % 2 == 1 ? 30 :31;
                }
                if(i == 2){
                    if(year % 4 == 0 && year % 100 != 0 || (year % 400 ==0 )){
                        days += 28;
                        monthDay = 29;
                    }else{
                        days += 28;
                        monthDay = 28;
                    }
                }
            }
            Show.mothed(days,monthDay);
        }else{
             System.out.println("请输入正确的月份!");
        }
    }
}

//显示日历
class Show
{
    public static void mothed(int days, int monthDay){
          //加一表示从星期一开始
          int week = (days - monthDay) % 7 + 1;

          System.out.println("\n周日"+"\t周一"+"\t周二"+"\t周三"+"\t周四"+"\t周五"+"\t周六");
          for (int i = 1;i <= monthDay + week ;i++ )
          {
              if(i <= week){
                  System.out.print(" "+"*"+"\t");
              }else{
                  if((i-week) < 10){
                       System.out.print(" "+(i-week)+"\t");
                    }else{
                       System.out.print((i-week)+"\t");
                     }
              }      
             if(i%7 == 0){
                  System.out.print("\n");
                 }
          }
    }
}

转载于:https://www.cnblogs.com/chenrenshui/p/6083022.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值