JAVA實現(xiàn)的簡單萬年歷代碼
更新時間:2015年10月21日 11:24:56 作者:hacker0825
這篇文章主要介紹了JAVA實現(xiàn)的簡單萬年歷代碼,涉及Java日期操作的相關(guān)技巧,具有一定參考借鑒價值,需要的朋友可以參考下
本文實例講述了JAVA實現(xiàn)的簡單萬年歷。分享給大家供大家參考,具體如下:
import java.util.Scanner; public class PrintCalendar { public static void main(String[] args) { int years = 0; int month = 0; int days = 0; boolean isRun = false; //從控制臺輸入年,月 Scanner input = new Scanner(System.in); System.out.print("請輸入年份:"); years = input.nextInt(); System.out.print("請輸入月份:"); month = input.nextInt(); System.out.println("\n*********"+years+"年"+month+"月 日曆表************"); //判斷是否是閏年 if((years % 4 == 0 && years % 100 != 0) || (years % 400 == 0)){ isRun = true; } int totalDays = 0; //累計天數(shù) //計算距離1900年1月1日的天數(shù) for(int i = 1900; i < years; i++){ if((i % 4 == 0 && i % 100 != 0) || (i % 400 == 0)){ totalDays = totalDays + 366; }else{ totalDays = totalDays + 365; } } int beforeDays = 0; //根據(jù)月份判斷天數(shù) for(int j = 1; j <= month; j++){ switch(j){ case 1: case 3: case 5: case 7: case 8: case 10: case 12: days = 31; break; case 4: case 6: case 9: case 11: days = 30; break; case 2: if(isRun){ days = 29; }else{ days = 28; } default: System.out.println("輸入月份不正確!!"); } if(j < month){ beforeDays = beforeDays + days; } } totalDays = totalDays + beforeDays; //統(tǒng)計到目前總天數(shù) int firstDayOfMonth = 0; int temp = 1 + totalDays % 7 ; if(temp == 7){ firstDayOfMonth = 0; //週日 }else{ firstDayOfMonth = temp; } /* 輸出日歷 */ System.out.println("星期日\t星期一\t星期二\t星期三\t星期四\t星期五\t星期六"); for(int k = 0; k < firstDayOfMonth; k++){ System.out.print("\t"); } for(int m = 1; m <= days; m++){ System.out.print( m + "\t"); if((totalDays + m) % 7 == 6){ System.out.print("\n"); } } } }
關(guān)于萬年歷的制作感興趣的朋友還可參考本站在線工具:
希望本文所述對大家Java程序設(shè)計有所幫助。
相關(guān)文章
idea 無法創(chuàng)建Scala class 選項的原因分析及解決辦法匯總
這篇文章主要介紹了idea 無法創(chuàng)建Scala class 選項的解決辦法匯總,本文給大家介紹的非常詳細,對大家的學(xué)習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-09-09java編寫創(chuàng)建數(shù)據(jù)庫和表的程序
這篇文章主要為大家詳細介紹了java編寫創(chuàng)建數(shù)據(jù)庫和表的程序,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-10-10關(guān)于Spring Cloud 本地屬性覆蓋的問題
這篇文章主要介紹了關(guān)于Spring Cloud 本地屬性覆蓋的問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-03-03