不久前曾介紹過
DateFormatSymbols可用來製作月曆等,今天就來DIY一個月曆。
這裡提供的程式式最基本的用法,若修改成JSP/custom tag形式,配合聯結資料庫,可進一步做出行事曆等多項應用。
import java.text.DateFormatSymbols; import java.text.DecimalFormat; import java.util.Arrays; import java.util.Calendar; import java.util.Date; import java.util.GregorianCalendar; import java.util.Locale;
public class MyCalendar {
private int firstDayOfWeek=0;
public static void main(String[] args) { showCalendar(2007,9,0); showCalendar(2007,10,1); }
/** * * @param year 年 * @param month 月 * @param firstDayOfWeek 每週第一天為 1 2 3 4 5 6 0 ,0=SUN */ private static void showCalendar(int year,int month,int firstDayOfWeek) { System.out.println(String.valueOf(year)+"年/"+String.valueOf(month)+"月"); String[] weekHeader=getCalendarWeekHeader(firstDayOfWeek); for(String day:weekHeader) { System.out.print(day+"\t"); } System.out.println(); String[][] content=getCalendarContent(year,month,firstDayOfWeek); for(String[] days:content) { for(String day:days) { System.out.print(day); } System.out.println(); } }
private static String[] getCalendarWeekHeader(int firstDay) { DateFormatSymbols smb=new DateFormatSymbols(Locale.getDefault()); String[] dayList=smb.getShortWeekdays(); // " ",sun,mon,tues......[] ,first element is blank string String[] newDayList=new String[dayList.length-1]; for(int i=0;i<7;i++,firstDay++) { if(firstDay==7)firstDay=0; newDayList[i]=dayList[firstDay+1]; } //re-order return newDayList; }
private static int[] getCalendarWeekIntArr(int firstDay) { int[] newIntDayList=new int[7]; for(int i=0;i<7;i++,firstDay++) { if(firstDay==7)firstDay=0; newIntDayList[i]=firstDay; } return newIntDayList; }
private static String[] getCalendarWeekHeader() { return getCalendarWeekHeader(0); }
private static String[][] getCalendarContent(int year,int month,int firstOfWeek) { GregorianCalendar calendar=new GregorianCalendar(Locale.getDefault()); calendar.set(year,month-1,1); // calc how many rows int maxDateOfThisMonth=calendar.getActualMaximum(calendar.DATE); //max date number of the month int minDateOfThisMonth=calendar.getActualMinimum(calendar.DATE); //min date number of the month calendar.set(calendar.DATE, maxDateOfThisMonth); calendar.setMinimalDaysInFirstWeek(1); //? calendar.setFirstDayOfWeek(firstOfWeek+1); int rowCount=calendar.get(calendar.WEEK_OF_MONTH); String[][] content=new String[rowCount][7];
//fill blank string to initial for(int i=0;i<rowCount;i++) { Arrays.fill(content[i], ""); }
//fill content int date=minDateOfThisMonth; int[] weekInts=getCalendarWeekIntArr(firstOfWeek); for(int row=0;row<rowCount;row++) //row { for(int col=0;col<7;col++) //column { if(date<=maxDateOfThisMonth) { calendar.set(calendar.DATE, date); if(calendar.get(calendar.DAY_OF_WEEK)==weekInts[col]+1) { content[row][col]=String.valueOf(date)+"\t"; date++; } else { content[row][col]="\t"; } } } } return content; } }
|
輸出結果:
星期日 星期一 星期二 星期三 星期四 星期五 星期六 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
2007年/10月 星期一 星期二 星期三 星期四 星期五 星期六 星期日 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 31
|
沒有留言:
張貼留言