源代码: package t3; import java.util.*; class DateExample { public static void main(String args[ ]) { Scanner read=new Scanner(System.in); System.out.println(\输入第一个日期的年份:\); int yearOne=read.nextInt( ); System.out.println(\输入该年的月份:\); int monthOne=read.nextInt( ); System.out.println(\输入该月份的日期:\); int dayOne=read.nextInt( ); System.out.println(\输入第二个日期的年份:\); int yearTwo=read.nextInt( ); System.out.println(\输入该年的月份:\); int monthTwo=read.nextInt( ); System.out.println(\输入该月份的日期:\); int dayTwo=read.nextInt( ); Calendar calendar=Calendar.getInstance(); //初始化日历对象。 calendar.set(yearOne, monthOne,dayOne); // 将calendar的时间设置为yearOne年monthOne月dayOne日 long timeOne=calendar.getTimeInMillis() ; // calendar表示的时间转换成毫秒。 calendar.set(yearTwo,monthTwo,dayTwo); // 将calendar的时间设置为yearTwo年monthTwo月dayTwo日 long timeTwo=calendar.getTimeInMillis(); // calendar表示的时间转换成毫秒 Date date1=new Date(timeOne); // 用timeOne做参数构造date1 Date date2=new Date(timeTwo); // 用timeTwo做参数构造date2 if(date2.equals(date1)) { System.out.println(\两个日期的年、月、日完全相同\); } else if(date2.after(date1)) { System.out.println(\您输入的第二个日期大于第一个日期\); } else if(date2.before(date1)) { System.out.println(\您输入的第二个日期小于第一个日期\); } long 相隔天数=(Math.abs(timeTwo-timeOne))/(1000*60*60*24); // 计算两个日期相隔天数
System.out.printf(\年%d月%d日和%d年%d月%d日相隔%d天\,yearOne,monthOne,dayOne, yearTwo,monthTwo,dayTwo, 相隔天数); } } 运行结果: *任务二:简单日历的实现 (1)内容 建立简单的日历,该日历能根据用户输入年月显示相应日历。 如输入: 2011 4 输出结果为: 2011- 4 日 一 二 三 四 五 六 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 (2)
源代码: package t31; import java.lang.*; import java.util.Scanner; public class Calendar { public static void main(String[] args) { // TODO Auto-generated method stub Scanner input = new Scanner(System.in); System.out.print(\请输入年份: \); int year = input.nextInt(); System.out.print(\请输入月份:\); int month = input.nextInt(); printMonth(year,month); } public static void printMonth(int year,int month) { //System.out.println(month + \ printMonthTitle(year,month); printMonthBody(year,month); } public static void printMonthTitle(int year,int month) { System.out.println(\ + getMonthName(month) + \ + year); System.out.println(\ ); System.out.println(\周日 周一 周二 周三 周四 周五 周六 \); } public static String getMonthName(int month) { String monthName = \; switch (month) { case 1: monthName = \一月\;break; case 2: monthName = \二月\;break; case 3: monthName = \三月\;break; case 4: monthName = \四月\;break; case 5: monthName = \五月\;break; case 6: monthName = \六月\;break; case 7: monthName = \七月\;break; case 8: monthName = \八月\;break; case 9: monthName = \九月\;break; case 10: monthName = \十月\;break; case 11: monthName = \十一月\;break; case 12: monthName = \十二月\; } return monthName; } public static void printMonthBody(int year,int month) { int startDay = getStartDay(year,month);
//System.out.println(\ int numberOfDaysInMonth = getNumberOfDaysInMonths(year,month); //System.out.println(\ for (int i = 0;i < startDay;i++){ System.out.print(\); //这里空格数是4,不对的话第一行位置会偏 } for (int i = 1;i < numberOfDaysInMonth + 1;i++){ //i=0,则日期从0开始,numberOfDaysInMonth + 1不加1则少一天 System.out.printf(\,i); if ((i + startDay) % 7 == 0){ System.out.println(); } } System.out.println(); } public static int getStartDay(int year,int month) { final int START_DAY_FOR_JAN_1_1800 = 4;//原 3 时间慢了一天 int totalNumberOfDay = getTotalNumberOfDays(year,month); return (totalNumberOfDay + START_DAY_FOR_JAN_1_1800) % 7; } public static int getTotalNumberOfDays(int year,int month) { int total = 0; for (int i = 0;i < 1800;i++){ if(isLeapYear(i)) { total = total + 366; } else { total = total + 365; } } for (int i = 0;i < month;i++) { total = total + getNumberOfDaysInMonths(year,i); } return total; } public static int getNumberOfDaysInMonths(int year,int month) { if ((month == 1) || (month == 3) || (month == 5) || (month == 7) || (month == 8) || (month == 10) || (month == 12)) { return 31; }