万年历java代码和理解 万年历的代码

关于用 java 语言编写的万年历的解读

sum+1就是你所要求的月份的第一天啊,你是算到前一个月底的天数了,得加一才是你所要求的月份的第一天。

你所需要的网站建设服务,我们均能行业靠前的水平为你提供.标准是产品质量的保证,主要从事网站设计、成都网站建设企业网站建设、手机网站制作、网页设计、品牌网站建设、网页制作、做网站、建网站。创新互联建站拥有实力坚强的技术研发团队及素养的视觉设计专才。

JAVA万年历代码

/*

题目:输出任意年份任意月份的日历表(公元后)

思路:

1.已知1年1月1日是星期日,1 % 7 = 1 对应的是星期日,2 % 7 = 2 对应的是星期一,以此类推;

2.计算当年以前所有天数+当年当月1号之前所有天数;

a.年份分平年闰年,平年365天,闰年366天;

b.闰年的判断方法year % 400 == 0 || (year % 100 != 0  year % 4 == 0)若为真,则为闰年否则为平年;

c.定义平年/闰年数组,包含各月天数;

d.遍历数组求和,计算当年当月前总天数;

e.当年以前所有天数+当年当月前总天数+1即为1年1月1日到当年当月1日的总天数;

3.总天数对7取模,根据结果判断当月1号是星期几,输出空白区域;

4.输出当月日历表,逢星期六换行

*/

import java.util.Scanner;

class FindMonthList {

public static void main(String[] args){

Scanner sc = new Scanner(System.in);

System.out.println("请输入年份:");

int year = sc.nextInt();            //年份

if (year  1) {                        //判断非法输入年份

System.out.println("输入错误!");

return;

}

System.out.println("请输入月份:");

int month = sc.nextInt();            //月份

if (month  1 || month  12) {        //判断非法输入月份

System.out.println("输入错误!");

return;

}

//输出表头

System.out.println("-------" + year + " 年 " + month + " 月 " + "-------");

System.out.println();

System.out.println("日  一  二  三  四  五  六");

//计算当前年份以前所有天数beforeYearTotalDay;每4年一个闰年,闰年366天,平年365天

int beforeYearTotalDay = ((year - 1) / 4 * 366) + (year-1 - ((year - 1) / 4)) * 365;

int[] arrLeapYear = {0,31,29,31,30,31,30,31,31,30,31,30,31};    //闰年各月天数    int数组

int[] arrNormalYear = {0,31,28,31,30,31,30,31,31,30,31,30,31};    //平年各月天数    int数组

int beforeMonthTotalDay = 0;                                    //定义本年当月之前月份的总天数

if (year % 400 == 0 || (year % 100 != 0  year % 4 == 0)) {    //判断当前年份是否是闰年

for (int i = 0 ; i  month ; i ++ ) {    //for循环计算当月之前总天数

//计算当前月份之前的所有天数

beforeMonthTotalDay = beforeMonthTotalDay + arrLeapYear[i];

}

//判断当月1日是星期几

int totalDay = beforeYearTotalDay + beforeMonthTotalDay + 1;

int week = totalDay % 7;//已知1年1月1日是星期日,即模7得1对应的是星期日

for (int i = 0 ; i  (week - 1 + 7) % 7 ; i ++) {    //如果写成i  (week-1)会出现i-1的情况

System.out.print("    ");//输出开头空白

}

for (int i = 1 ;i = arrLeapYear[month] ;i ++ ) {    //for循环输出各月天数

System.out.print(i + "  ");

if (i  10 ) {        //小于10的数补一个空格,以便打印整齐

System.out.print(" ");

}

if (i % 7 == ((7-(week - 1)) % 7 ) || i == arrLeapYear[month]) {//每逢星期六/尾数换行

System.out.println();

}

}

} else {        //不是闰年就是平年

for (int i = 0 ; i  month ; i ++ ) {    //for循环计算出当月之前月份总天数

beforeMonthTotalDay = beforeMonthTotalDay + arrNormalYear[i];

}

//判断当月1日是星期几

int totalDay = beforeYearTotalDay + beforeMonthTotalDay + 1;

int week = totalDay % 7;//已知1年1月1日是星期日,即模7得1对应的是星期日

for (int i = 0 ; i  (week - 1 + 7) % 7 ; i ++) {    //如果写成i  (week-1)会出现i-1的情况

System.out.print("    ");//输出开头空白

}

for (int i = 1 ;i = arrNormalYear[month] ;i ++ ) {//for循环输出各月天数

System.out.print(i + "  ");

if (i  10 ) {            //小于10的数补一个空格,以便打印整齐

System.out.print(" ");

}

if (i % 7 == ((7-(week - 1)) % 7 ) || i == arrNormalYear[month]) {//每逢星期六/尾数换行

System.out.println();

}

}

}

}

}

显示效果:

JAVA编写一个多功能万年历程序

import java.text.SimpleDateFormat; import java.util.Calendar; public class TestDate { public static final String[] weeks = { "日", "一", "二", "三", "四", "五", "六" }; public static void main(String[] args) { Calendar c = Calendar.getInstance(); c.set(Calendar.YEAR,2011);//2011年 c.set(Calendar.MONTH,0);//java中Calendar类,月从0开始, 0代表一月 c.set(Calendar.DATE,1);//1号 int day = c.get(Calendar.DAY_OF_WEEK);//获致是本周的第几天地, 1代表星期天...7代表星期六 System.out.println(new SimpleDateFormat( "yyyy-MM-dd ").format(c.getTime())); System.out.println("星期" + weeks[day-1]); } } 把以上测试代码写作一个方法 方法的参数名为年月日, 即可。当然Calendar 还有很多功能,比如一周的第几天,一年的第几个月……

文章题目:万年历java代码和理解 万年历的代码
本文地址:https://www.cdcxhl.com/article26/dodddcg.html

成都网站建设公司_创新互联,为您提供营销型网站建设网站制作软件开发全网营销推广网站策划网站内链

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联

成都seo排名网站优化