java日曆顯示年份、月份

來源:互聯網
上載者:User

標籤:port   --   日期   out   parse   ati   截取   arm   輸入   

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日曆顯示年份、月份

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.