form.jspx
<?xml version="1.0" encoding="windows-1256" ?>
<!-- Persian Calendar Demo 2005-02-21 14:25:46 Copyright © Ghasem Kiani <ghasemkiani@yahoo.com> This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA -->
<jsp:root version="2.0" xmlns="http://www.w3.org/1999/xhtml" xmlns:c="http://java.sun.com/jsp/jstl/core" xmlns:fn="http://java.sun.com/jsp/jstl/functions" xmlns:jsp="http://java.sun.com/JSP/Page">
<jsp:directive.page contentType="text/html" />
<c:set property="characterEncoding" target="${pageContext.request}" value="UTF-8" />
<c:set property="characterEncoding" target="${pageContext.response}" value="UTF-8" />
<jsp:scriptlet>
<![CDATA[
Object[][] fields =
{
{"Year", new Integer(com.ibm.icu.util.Calendar.YEAR)},
{"Month", new Integer(com.ibm.icu.util.Calendar.MONTH)},
{"Week of Month", new Integer(com.ibm.icu.util.Calendar.WEEK_OF_MONTH)},
{"Week of Year", new Integer(com.ibm.icu.util.Calendar.WEEK_OF_YEAR)},
{"Day of Month", new Integer(com.ibm.icu.util.Calendar.DAY_OF_MONTH)},
{"Day of Week", new Integer(com.ibm.icu.util.Calendar.DAY_OF_WEEK)},
{"Day of Week in Month", new Integer(com.ibm.icu.util.Calendar.DAY_OF_WEEK_IN_MONTH)},
{"Day of Year", new Integer(com.ibm.icu.util.Calendar.DAY_OF_YEAR)},
{"AM/PM", new Integer(com.ibm.icu.util.Calendar.AM_PM)},
{"Hour of day", new Integer(com.ibm.icu.util.Calendar.HOUR_OF_DAY)},
{"Hour", new Integer(com.ibm.icu.util.Calendar.HOUR)},
{"Minute", new Integer(com.ibm.icu.util.Calendar.MINUTE)},
{"Second", new Integer(com.ibm.icu.util.Calendar.SECOND)}
};
pageContext.setAttribute("fields", fields);
Object[][] formats =
{
{"Default", new Integer(com.ibm.icu.text.DateFormat.DEFAULT)},
{"Short", new Integer(com.ibm.icu.text.DateFormat.SHORT)},
{"Medium", new Integer(com.ibm.icu.text.DateFormat.MEDIUM)},
{"Long", new Integer(com.ibm.icu.text.DateFormat.LONG)},
{"Full", new Integer(com.ibm.icu.text.DateFormat.FULL)}
};
pageContext.setAttribute("formats", formats);
String tz = String.valueOf(request.getParameter("timeZone"));
if(tz == null || "null".equals(tz) || tz.length() == 0) tz = com.ibm.icu.util.TimeZone.getDefault().getID();
pageContext.setAttribute("tz", tz);
String[] tzs = com.ibm.icu.util.TimeZone.getAvailableIDs();
pageContext.setAttribute("tzs", tzs);
com.ibm.icu.util.TimeZone timeZone = com.ibm.icu.util.TimeZone.getTimeZone(tz);
pageContext.setAttribute("timeZone", timeZone);
String loc = String.valueOf(request.getParameter("locale"));
if(loc == null || "null".equals(loc) || loc.length() == 0) loc = com.ibm.icu.util.ULocale.getDefault().toString();
pageContext.setAttribute("loc", loc);
com.ibm.icu.util.ULocale[] locs = com.ibm.icu.util.ULocale.getAvailableLocales();
pageContext.setAttribute("locs", locs);
com.ibm.icu.util.ULocale uLocale = com.ibm.icu.util.ULocale.createCanonical(loc);
pageContext.setAttribute("uLocale", uLocale);
com.ibm.icu.util.Calendar calendar;
String calendarType = String.valueOf(request.getParameter("calendarType"));
pageContext.setAttribute("calendarType", calendarType);
if("Persian".equals(calendarType)) calendar = new com.ghasemkiani.util.icu.PersianCalendar(timeZone, uLocale);
else if("Islamic".equals(calendarType)) calendar = new com.ibm.icu.util.IslamicCalendar(timeZone, uLocale);
else if("Gregorian".equals(calendarType)) calendar = new com.ibm.icu.util.GregorianCalendar(timeZone, uLocale);
else if("Japanese".equals(calendarType)) calendar = new com.ibm.icu.util.JapaneseCalendar(timeZone, uLocale);
else if("Chinese".equals(calendarType)) calendar = new com.ibm.icu.util.ChineseCalendar(timeZone, uLocale);
else if("Buddhist".equals(calendarType)) calendar = new com.ibm.icu.util.BuddhistCalendar(timeZone, uLocale);
else if("Hebrew".equals(calendarType)) calendar = new com.ibm.icu.util.HebrewCalendar(timeZone, uLocale);
else calendar = new com.ibm.icu.util.GregorianCalendar(timeZone, uLocale);
pageContext.setAttribute("calendar", calendar);
boolean applyTimeInMillis = "on".equals(String.valueOf(request.getParameter("applyTimeInMillis")));
pageContext.setAttribute("applyTimeInMillis", new Boolean(applyTimeInMillis));
if(applyTimeInMillis)
{
try{ calendar.setTimeInMillis(Long.parseLong(request.getParameter("timeInMillis"))); } catch(Exception e){ }
}
boolean applyDate = "on".equals(String.valueOf(request.getParameter("applyDate")));
pageContext.setAttribute("applyDate", new Boolean(applyDate));
if(applyDate)
{
try{ calendar.set(calendar.EXTENDED_YEAR, Integer.parseInt(request.getParameter("year"))); } catch(Exception e){ }
try{ calendar.set(calendar.MONTH, Integer.parseInt(request.getParameter("month")) - 1); } catch(Exception e){ }
try{ calendar.set(calendar.DAY_OF_MONTH, Integer.parseInt(request.getParameter("day"))); } catch(Exception e){ }
try{ calendar.set(calendar.HOUR_OF_DAY, Integer.parseInt(request.getParameter("hour"))); } catch(Exception e){ }
try{ calendar.set(calendar.MINUTE, Integer.parseInt(request.getParameter("minute"))); } catch(Exception e){ }
try{ calendar.set(calendar.SECOND, Integer.parseInt(request.getParameter("second"))); } catch(Exception e){ }
}
boolean applyAdd = "on".equals(String.valueOf(request.getParameter("applyAdd")));
pageContext.setAttribute("applyAdd", new Boolean(applyAdd));
int fieldAdd = 0;
int amountAdd = 0; try
{
fieldAdd = Integer.parseInt(request.getParameter("fieldAdd"));
amountAdd = Integer.parseInt(request.getParameter("amountAdd"));
if(applyAdd) calendar.add(fieldAdd, amountAdd);
}
catch(Exception e)
{
pageContext.setAttribute("errorAdd", e);
}
pageContext.setAttribute("fieldAdd", new Integer(fieldAdd));
pageContext.setAttribute("amountAdd", new Integer(amountAdd));
boolean applyRoll = "on".equals(String.valueOf(request.getParameter("applyRoll")));
pageContext.setAttribute("applyRoll", new Boolean(applyRoll));
int fieldRoll = 0;
int amountRoll = 0; try
{
fieldRoll = Integer.parseInt(request.getParameter("fieldRoll"));
amountRoll = Integer.parseInt(request.getParameter("amountRoll"));
if(applyRoll) calendar.roll(fieldRoll, amountRoll);
}
catch(Exception e)
{
pageContext.setAttribute("errorRoll", e);
}
pageContext.setAttribute("fieldRoll", new Integer(fieldRoll));
pageContext.setAttribute("amountRoll", new Integer(amountRoll));
pageContext.setAttribute("timeInMillis", new Long(calendar.getTimeInMillis()));
pageContext.setAttribute("year", new Integer(calendar.get(calendar.EXTENDED_YEAR)));
pageContext.setAttribute("month", new Integer(calendar.get(calendar.MONTH) + 1));
pageContext.setAttribute("day", new Integer(calendar.get(calendar.DAY_OF_MONTH)));
pageContext.setAttribute("hour", new Integer(calendar.get(calendar.HOUR_OF_DAY)));
pageContext.setAttribute("minute", new Integer(calendar.get(calendar.MINUTE)));
pageContext.setAttribute("second", new Integer(calendar.get(calendar.SECOND)));
int formatDate = 0;
try{ formatDate = Integer.parseInt(request.getParameter("formatDate")); } catch(Exception e){ }
pageContext.setAttribute("formatDate", new Integer(formatDate));
int formatTime = 0;
try{ formatTime = Integer.parseInt(request.getParameter("formatTime")); } catch(Exception e){ }
pageContext.setAttribute("formatTime", new Integer(formatTime));
boolean applyPattern = "on".equals(String.valueOf(request.getParameter("applyPattern")));
pageContext.setAttribute("applyPattern", new Boolean(applyPattern));
String pattern = "";
try{ pattern = String.valueOf(request.getParameter("pattern")); } catch(Exception e){ }
if(pattern == null || "null".equals(pattern)) pattern = "";
pageContext.setAttribute("pattern", pattern);
com.ibm.icu.text.SimpleDateFormat sdf = (com.ibm.icu.text.SimpleDateFormat)calendar.getDateTimeFormat(formatDate, formatTime, uLocale);
if(applyPattern && pattern.length() > 0) sdf.applyPattern(pattern);
String result = sdf.format(calendar.getTime());
pageContext.setAttribute("result", result);
]]>
</jsp:scriptlet>
<html>
<head>
<meta content="text/html; charset=${pageContext.response.characterEncoding}" http-equiv="Content-Type" />
<title>
Persian Calendar Demo
</title>
</head>
<body>
<c:url scope="page" value="/form.jspx" var="url" />
<form action="${url}" method="post">
<table style="border: solid red 1px;">
<tr>
<td colspan="3" style="text-align: center; background: lightyellow;">
<span style="font-size: 150%; font-weight: bold;">
PERSIAN CALENDAR DEMO
</span>
<br />
<span style="font-size: 75%; font-style: italic;">
Ghasem Kiani, M.D.
</span>
</td>
</tr>
<tr>
<td style="background: #eeeeee;">
Time Zone
</td>
<td>
<select name="timeZone">
<c:forEach items="${tzs}" var="item">
<c:choose>
<c:when test="${tz eq item}">
<option selected="selected">
${item}
</option>
</c:when>
<c:otherwise>
<option>
${item}
</option>
</c:otherwise>
</c:choose>
</c:forEach>
</select>
</td>
<td />
</tr>
<tr>
<td style="background: #eeeeee;">
Locale
</td>
<td>
<select name="locale">
<c:forEach items="${locs}" var="item">
<c:choose>
<c:when test="${loc eq item}">
<option selected="selected">
${item}
</option>
</c:when>
<c:otherwise>
<option>
${item}
</option>
</c:otherwise>
</c:choose>
</c:forEach>
</select>
</td>
<td />
</tr>
<tr>
<td style="background: #eeeeee;">
Calendar Type
</td>
<td>
<select name="calendarType">
<c:forTokens delims="," items="Gregorian,Persian,Islamic,Japanese,Chinese,Buddhist,Hebrew" var="item">
<c:choose>
<c:when test="${calendarType eq item}">
<option selected="selected">
${item}
</option>
</c:when>
<c:otherwise>
<option>
${item}
</option>
</c:otherwise>
</c:choose>
</c:forTokens>
</select>
</td>
<td />
</tr>
<tr>
<td style="background: #eeeeee;">
Time in Millis
</td>
<td>
<input name="timeInMillis" value="${timeInMillis}" />
</td>
<td>
<c:choose>
<c:when test="${applyTimeInMillis}">
<input checked="checked" name="applyTimeInMillis" type="checkbox" />
</c:when>
<c:otherwise>
<input name="applyTimeInMillis" type="checkbox" />
</c:otherwise>
</c:choose>
Apply
</td>
</tr>
<tr>
<td style="background: #eeeeee;">
Date
</td>
<td>
<input name="year" style="width: 2.5em;" value="${year}" />
/
<input name="month" style="width: 1.5em;" value="${month}" />
/
<input name="day" style="width: 1.5em;" value="${day}" />
-
<input name="hour" style="width: 1.5em;" value="${hour}" />
:
<input name="minute" style="width: 1.5em;" value="${minute}" />
:
<input name="second" style="width: 1.5em;" value="${second}" />
</td>
<td>
<c:choose>
<c:when test="${applyDate}">
<input checked="checked" name="applyDate" type="checkbox" />
</c:when>
<c:otherwise>
<input name="applyDate" type="checkbox" />
</c:otherwise>
</c:choose>
Apply
</td>
</tr>
<tr>
<td style="background: #eeeeee;">
Add
</td>
<td>
<select name="fieldAdd">
<c:forEach items="${fields}" var="item">
<c:choose>
<c:when test="${fieldAdd eq item[1]}">
<option selected="selected" value="${item[1]}">
${item[0]}
</option>
</c:when>
<c:otherwise>
<option value="${item[1]}">
${item[0]}
</option>
</c:otherwise>
</c:choose>
</c:forEach>
</select>
by
<input name="amountAdd" style="width: 3em;" value="${amountAdd}" />
</td>
<td>
<c:choose>
<c:when test="${applyAdd}">
<input checked="checked" name="applyAdd" type="checkbox" />
</c:when>
<c:otherwise>
<input name="applyAdd" type="checkbox" />
</c:otherwise>
</c:choose>
Apply
</td>
</tr>
<tr>
<td style="background: #eeeeee;">
Roll
</td>
<td>
<select name="fieldRoll">
<c:forEach items="${fields}" var="item">
<c:choose>
<c:when test="${fieldRoll eq item[1]}">
<option selected="selected" value="${item[1]}">
${item[0]}
</option>
</c:when>
<c:otherwise>
<option value="${item[1]}">
${item[0]}
</option>
</c:otherwise>
</c:choose>
</c:forEach>
</select>
by
<input name="amountRoll" style="width: 3em;" value="${amountRoll}" />
</td>
<td>
<c:choose>
<c:when test="${applyRoll}">
<input checked="checked" name="applyRoll" type="checkbox" />
</c:when>
<c:otherwise>
<input name="applyRoll" type="checkbox" />
</c:otherwise>
</c:choose>
Apply
</td>
</tr>
<tr>
<td style="background: #eeeeee;">
Style
</td>
<td>
Date:
<select name="formatDate">
<c:forEach items="${formats}" var="item">
<c:choose>
<c:when test="${formatDate eq item[1]}">
<option selected="selected" value="${item[1]}">
${item[0]}
</option>
</c:when>
<c:otherwise>
<option value="${item[1]}">
${item[0]}
</option>
</c:otherwise>
</c:choose>
</c:forEach>
</select>
Time:
<select name="formatTime">
<c:forEach items="${formats}" var="item">
<c:choose>
<c:when test="${formatTime eq item[1]}">
<option selected="selected" value="${item[1]}">
${item[0]}
</option>
</c:when>
<c:otherwise>
<option value="${item[1]}">
${item[0]}
</option>
</c:otherwise>
</c:choose>
</c:forEach>
</select>
</td>
<td />
</tr>
<tr>
<td style="background: #eeeeee;">
Pattern
</td>
<td>
<input name="pattern" style="width: 100%;" value="${pattern}" />
</td>
<td>
<c:choose>
<c:when test="${applyPattern}">
<input checked="checked" name="applyPattern" type="checkbox" />
</c:when>
<c:otherwise>
<input name="applyPattern" type="checkbox" />
</c:otherwise>
</c:choose>
Apply
</td>
</tr>
<tr>
<td colspan="3">
<p style="text-align: center;">
<input type="submit" value="Refresh" />
</p>
</td>
</tr>
<tr>
<td colspan="3" dir="${fn:containsIgnoreCase('fa,ar,iw,he,ku,ps', uLocale.language)? 'rtl': 'ltr'}" style="background: lavender; font-family: Tahoma;">
${result}
</td>
</tr>
</table>
</form>
<p />
</body>
</html>
</jsp:root>