zoukankan      html  css  js  c++  java
  • java编程实现日历

    package com.beiwo.other;
    /*
    * 需求:输入一个年份和月份 ,显示当前月日情况 ,星期数要对应准确
    * 1.1900年1月1号开始
    * 2.星期 : 直接用总天数对7求余数 31 28 59 / 7 = 5
    * 3.以 来个开
    */
    public class Demo4 {

    /**
    * @param args
    */
    public static void main(String[] args) {
    // TODO Auto-generated method stub

    Demo4 demo = new Demo4();

    //输入你要查询的年月

    demo.show(1900,3);

    }

    //定义一个工具方法:判断平年还是闰年
    public boolean isLeapYear(int year){

    if(year % 400 == 0 || (year % 4==0 && year % 100 != 0)){

    return true;
    }

    return false;
    }

    //计算输入年份下,月份的总天数 1900

    public int getTotalDateFrom(int year , int month){

    //1.定年份的总天数
    int totalDate = 0;//1900到你输入的年份的总天数 2016 1900 - 1903 = 3
    for(int i= 1900 ; i < year ;i++){

    if(isLeapYear(i)){ // 闰年

    totalDate += 366;
    }else {

    totalDate += 365;
    }
    }

    //2.计算月份的天数
    for(int i = 1 ; i< month ;i++){

    totalDate += getDayOfMonth(year, i);
    }

    return totalDate;

    }



    public int getDayOfMonth(int year , int month){

    switch (month) {
    case 1:
    case 3:
    case 5:
    case 7:
    case 8:
    case 10:
    case 12:

    return 31;

    case 4:
    case 6:
    case 9:
    case 11:
    return 30;
    case 2:
    if(isLeapYear(year)){

    return 29;
    }else {

    return 28;
    }
    default:
    return 30;
    }
    }

    public int printSpace(int year , int month){

    return getTotalDateFrom(year, month) % 7;
    }

    public void print(int year , int month){

    int countSpac = printSpace(year, month) + 1; // 空格的个数
    int count = 0; //定义一个计数
    for(int i = 1; i<=countSpac; i++){
    count++;
    System.out.print(" ");
    }

    //2.打印日期
    for(int i = 1 ; i<=getDayOfMonth(year, month);i++){

    if(count % 7 == 0){

    System.out.println();
    }
    count++;
    System.out.print(i+" ");

    }

    }

    public void show(int year , int month){

    System.out.println("******************"+year+"****"+month+"***************");
    System.out.println("======================================================");
    System.out.println("日 一 二 三 四 五 六");
    System.out.println("======================================================");
    print(year, month);
    System.out.println();
    }

    }

    //效果预览

    //

  • 相关阅读:
    第一个Django demo
    内建函数
    Git积累
    区间dp
    【Spring】Spring AOP详解(转载)
    【git】git 常用命令(含删除文件)
    【idea】idea如何在maven工程中引入jar包
    【Java】Scanner类nextInt后使用nextLine无法读取输入
    【Java_Eclipse】Eclipse插件如何卸载?
    【MySQL】MySQL5.7等以上版本在Windows上的配置
  • 原文地址:https://www.cnblogs.com/shen-xiao-jie/p/6104722.html
Copyright © 2011-2022 走看看