星空网 > 软件开发 > Java

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




原标题:java日历显示年份、月份

关键词:JAVA

*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

亚马逊解封冻结资金!复盘中国卖家胜诉的关键点:https://www.ikjzd.com/articles/1578632108017348609
跨境店铺选品要注意哪些要素:https://www.ikjzd.com/articles/1578632559383179266
这类卖家有望爆单!世界杯周边产品成紧俏货!:https://www.ikjzd.com/articles/1578635446385876993
1美分包邮的Temu口碑下滑,拼多多再出狠招迎合美国市场!:https://www.ikjzd.com/articles/1578637220416745474
海外电商购物季怎么也越来越早了?:https://www.ikjzd.com/articles/1578638460747935746
海外版抖音TikTok特赚钱的16个热门类目!普通人也可以做!:https://www.ikjzd.com/articles/1578641035971239938
响沙湾门票套票价格大介绍(省钱攻略必备):https://www.vstour.cn/a/404238.html
上海到镇江茅山旅游攻略 上海到镇江茅山旅游攻略路线:https://www.vstour.cn/a/404239.html
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流