All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.darwinsys.servlet.HTMLDateUtils Maven / Gradle / Ivy

There is a newer version: 1.8.0
Show newest version
package com.darwinsys.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Calendar;

import com.darwinsys.util.DateUtils;

/**
 * Utilities for dealing with Dates in the context of Servlets;
 * the print...Calendar routines depend on "calendar.css" being
 * extracted from the distribution and copied to the root of the
 * web application; without this it'll work but but look coyote ugly.
 */
public class HTMLDateUtils {

	/**
	 * Generate a small HTML month calendar for the current month onto the given PrintWriter
	 * @param out The PrintWriter
	 * @throws IOException
	 */
	public static void printMonthCalendar(PrintWriter out)
	throws IOException {
		printMonthCalendar(out, Calendar.getInstance());
	}

	/**
	 * Generate a small HTML month calendar for the given month onto the given PrintWriter
	 */
	public static void printMonthCalendar(PrintWriter out, Calendar calendar)
	throws IOException {

		int yy = calendar.get(Calendar.YEAR);
		int mm = calendar.get(Calendar.MONTH);
		int day = calendar.get(Calendar.DAY_OF_MONTH);

		out.println("");

		out.println("");
		out.println("");

		out.println("");

		//	Compute how much to leave before the first day.
		//	getDay() returns 0 for Sunday, which is just right.
		int leadGap = calendar.get(Calendar.DAY_OF_WEEK) - 1;

		int daysInMonth = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);

		out.print("");

		//	Blank out the labels before 1st day of month
		for (int i = 0; i < leadGap; i++) {
			out.print("");

			if ((leadGap + i) % 7 == 0) { // wrap if end of line.
				out.println("");
				out.print("");
			}
		}

		out.println("");
		out.println("
" + DateUtils.getMonthName(mm) + ", " + yy + "
SuMoTuWeThFrSa
 "); } // Fill in numbers for the day of month. for (int i = 1; i <= daysInMonth; i++) { out.printf("", i != day ? "cal-cell-day" : "cal-cell-today"); out.print(i); out.print("
"); } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy