首页 > 代码库 > 第四章:显示当月日期的小程序。
第四章:显示当月日期的小程序。
1 package cha4; 2 3 import java.text.DateFormatSymbols; 4 import java.util.Calendar; 5 import java.util.GregorianCalendar; 6 7 public class CalendarTest { 8 9 public static void main(String[] args) {10 GregorianCalendar today = new GregorianCalendar(2014,10,30) ;11 12 int curMonth = today.get(Calendar.MONTH) ;13 today.set(Calendar.DAY_OF_MONTH, 1);14 15 int weekday = today.getFirstDayOfWeek() ;//一个星期的星期几为第一天16 int curWeekday = today.get(Calendar.DAY_OF_WEEK) ;//今天为这个星期的第几天17 int indent = 0 ; //缩进,显示上个月的日期18 19 while(weekday != curWeekday) {20 today.add(Calendar.DAY_OF_MONTH, -1);21 curWeekday = today.get(Calendar.DAY_OF_WEEK) ;22 indent++ ;23 }24 25 String[] weekdayNames = new DateFormatSymbols().getShortWeekdays() ; 26 27 for (String weekdayName : weekdayNames) {28 if(!"".equals(weekdayName.trim()))29 System.out.print(weekdayName + "\t");30 }31 32 System.out.println();33 34 for(int i = 0 ; i< indent ;i++) {//打印上个月的几天35 System.out.print(today.get(Calendar.DAY_OF_MONTH) + "\t\t");36 today.add(Calendar.DAY_OF_MONTH, 1);37 }38 39 do{40 int day = today.get(Calendar.DAY_OF_MONTH) ;41 System.out.print(day + "\t\t");42 today.add(Calendar.DAY_OF_MONTH, 1) ;43 curWeekday = today.get(Calendar.DAY_OF_WEEK) ;44 if(curWeekday == weekday)45 System.out.println();46 }while(today.get(Calendar.MONTH) == curMonth) ;47 48 }49 }
第四章:显示当月日期的小程序。
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。