package org.crosswire.html; import java.util.GregorianCalendar; import java.util.Calendar; import java.util.Date; import java.util.Vector; import java.text.SimpleDateFormat; public class CalendarHTML { private Date curDate = null; private boolean week = false; private boolean month = false; public CalendarHTML() { this(new Date()); } public CalendarHTML(Date curDate) { init(curDate, false, false); } private void init(Date curDate, boolean week, boolean month) { this.curDate = (curDate == null) ? new Date() : curDate; this.week = week; this.month = month; } public String getEventID(int year, int month, int day) { return String.valueOf(year + (month*100) + day); } public String getActiveDayHTML(String body, String baseURL, String eventID) { return ""; } public String getHeaderHTML(Date curMonth, String baseURL) { return "

"+new SimpleDateFormat("MMMM yyyy").format(curMonth)+"

"; } public String getDayRowHTML(Date curMonth, String baseURL) { return "SMTWTFS"; } public String getMiniMonth(String baseURL) { Calendar cal = GregorianCalendar.getInstance(); cal.setTime(curDate); Calendar today = GregorianCalendar.getInstance(); today.setTime(new Date()); // today.set(Calendar.HOUR_OF_DAY, 0); // today.set(Calendar.MINUTE, 0); // today.set(Calendar.SECOND, 0); Calendar weekStart = (Calendar) cal.clone(); weekStart.set(Calendar.DAY_OF_MONTH, weekStart.get(Calendar.DAY_OF_MONTH) - (weekStart.get(Calendar.DAY_OF_WEEK)-1)); weekStart.set(Calendar.HOUR_OF_DAY, 0); weekStart.set(Calendar.MINUTE, 0); weekStart.set(Calendar.SECOND, 0); Calendar weekEnd = (Calendar) weekStart.clone(); weekEnd.set(Calendar.DAY_OF_MONTH, weekEnd.get(Calendar.DAY_OF_MONTH) + 7); weekEnd.set(Calendar.HOUR_OF_DAY, 23); weekEnd.set(Calendar.MINUTE, 59); weekEnd.set(Calendar.SECOND, 59); Date saveDate = curDate; StringBuffer buf = new StringBuffer(); buf.append(""); buf.append(""); buf.append(""); buf.append(""); buf.append("
"); buf.append(getHeaderHTML(cal.getTime(), baseURL)); buf.append("
"); buf.append(""); buf.append(getDayRowHTML(cal.getTime(), baseURL)); cal.setTime(new Date()); int now_monthday = cal.get(cal.DAY_OF_MONTH); int now_month = cal.get(cal.MONTH); cal.setTime(curDate); int cursel_monthday = cal.get(cal.DAY_OF_MONTH); int cursel_month = cal.get(cal.MONTH); cal.set(cal.DAY_OF_MONTH, 1); cal.set(cal.DAY_OF_MONTH, 2-cal.get(cal.DAY_OF_WEEK)); for (int i = 0; i < 6; i++) { buf.append(""); for (int j = 0; j < 7; j++) { // boolean cursel = ((cal.get(cal.DAY_OF_MONTH) == cursel_monthday) // && (cal.get(cal.MONTH) == cursel_month) // && (!week) && (!month) && (j < 7)); String showID = getEventID(cal.get(cal.YEAR), cal.get(cal.MONTH), cal.get(cal.DAY_OF_MONTH)); boolean cursel = (showID != null); boolean otherMonth = (cal.get(cal.MONTH) != cursel_month); boolean now = ((cal.get(cal.DAY_OF_MONTH) == now_monthday) && (cal.get(cal.MONTH) == now_month)); boolean prevDays = false; //cal.before(today); if (j == 7) { if (cal.before(weekEnd) && cal.after(weekStart) && (week)) { cursel = true; } } buf.append(""); cal.set(cal.DAY_OF_MONTH, cal.get(cal.DAY_OF_MONTH)+1); } buf.append(""); if ((i == 4) && (cal.get(cal.DAY_OF_MONTH) < 20)) // if we've rolled over already, no need for a 6th week break; } buf.append("
"); if ((i > 0) && (j==7)) { // if first week, then last day of week is good // buf if NOT first week, then we should select first day of week saveDate = cal.getTime(); cal.set(Calendar.DAY_OF_MONTH, cal.get(Calendar.DAY_OF_MONTH) - (cal.get(Calendar.DAY_OF_WEEK)-1)); } if ((i > 0) && (j==7)) { // set back out current date cal.setTime(saveDate); } if (cursel) buf.append(getActiveDayHTML(String.valueOf(cal.get(cal.DAY_OF_MONTH)), baseURL, showID)); else { buf.append(cal.get(cal.DAY_OF_MONTH)); } buf.append("
"); return buf.toString(); } public static void main(String [] argv) { CalendarHTML self = new CalendarHTML(new Date()); self.getMiniMonth(""); Vector x = new Vector(); } }