Java/JSTL/Date — различия между версиями

Материал из Java эксперт
Перейти к: навигация, поиск
 
м (1 версия)
 
(нет различий)

Текущая версия на 06:17, 1 июня 2010

Date Formating in JSTL

<%@ taglib uri="http://java.sun.ru/jstl/core" prefix="c" %><%@ taglib uri="http://java.sun.ru/jstl/fmt" prefix="fmt" %>
<html>
  <head>
    <title>Calendar</title>
  </head>
  <body>
    <form method="POST">
      <table border="1" cellpadding="0" cellspacing="0"
      style="border-collapse: collapse" bordercolor="#111111"
      width="62%" id="AutoNumber1">
        <tr>
          <td width="100%" colspan="2" bgcolor="#0000FF">
            <p align="center">
              <b>
                <font color="#FFFFFF" size="4">Date
                Formatting</font>
              </b>
            </p>
          </td>
        </tr>
        <tr>
          <td width="47%">Enter a month(1-12)</td>
          <td width="53%">
            <input type="text" name="month" size="20" />
          </td>
        </tr>
        <tr>
          <td width="47%">Enter a year(i.e. 2002)</td>
          <td width="53%">
            <input type="text" name="year" size="20" />
          </td>
        </tr>
        <tr>
          <td width="100%" colspan="2">
            <p align="center">
              <input type="submit" value="Submit" name="submit" />
              <input type="reset" value="Reset" name="reset" />
            </p>
          </td>
        </tr>
      </table>
      <p>&#160;</p>
    </form>
    <c:if test="${pageContext.request.method=="POST"}">
      <table border="1" cellpadding="0" cellspacing="0"
      style="border-collapse: collapse" bordercolor="#111111"
      width="63%" id="AutoNumber2">
        <fmt:parseDate var="now"
        value="${param.month}/1/${param.year}" type="date"
        dateStyle="short" />
        <tr>
          <td width="100%" colspan="7" bgcolor="#0000FF">
            <p align="center">
              <b>
                <font color="#FFFFFF" size="4">
                  <fmt:formatDate pattern="MMMMM yyyy"
                  value="${now}" />
                </font>
              </b>
            </p>
          </td>
        </tr>
        <fmt:formatDate var="i" pattern="E" value="${now}" />
        <c:choose>
          <c:when test="${i=="Sun"}">
            <c:set var="i" value="1" />
          </c:when>
          <c:when test="${i=="Mon"}">
            <c:set var="i" value="2" />
          </c:when>
          <c:when test="${i=="Tue"}">
            <c:set var="i" value="3" />
          </c:when>
          <c:when test="${i=="Wed"}">
            <c:set var="i" value="4" />
          </c:when>
          <c:when test="${i=="Thu"}">
            <c:set var="i" value="5" />
          </c:when>
          <c:when test="${i=="Fri"}">
            <c:set var="i" value="6" />
          </c:when>
          <c:when test="${i=="Sat"}">
            <c:set var="i" value="7" />
          </c:when>
          <c:otherwise>
            <c:set var="i" value="?" />
          </c:otherwise>
        </c:choose>
        <c:choose>
          <c:when test="${param.month==2}">
            <c:set var="max" value="28" />
            <c:if
            test="${ ((param.year % 4 == 0 && paran.year % 100 != 0) || param.year % 400 == 0) }">
              <c:set var="max" value="29" />
            </c:if>
          </c:when>
          <c:when test="${param.month==4}">
            <c:set var="max" value="30" />
          </c:when>
          <c:when test="${param.month==6}">
            <c:set var="max" value="30" />
          </c:when>
          <c:when test="${param.month==9}">
            <c:set var="max" value="30" />
          </c:when>
          <c:when test="${param.month==11}">
            <c:set var="max" value="30" />
          </c:when>
          <c:otherwise>
            <c:set var="max" value="31" />
          </c:otherwise>
        </c:choose>
        <tr>
          <td width="70">
            <b>
              <center>Sunday</center>
            </b>
          </td>
          <td width="70">
            <b>
              <center>Monday</center>
            </b>
          </td>
          <td width="70">
            <b>
              <center>Tuesday</center>
            </b>
          </td>
          <td width="70">
            <b>
              <center>Wednesday</center>
            </b>
          </td>
          <td width="70">
            <b>
              <center>Thursday</center>
            </b>
          </td>
          <td width="70">
            <b>
              <center>Friday</center>
            </b>
          </td>
          <td width="70">
            <b>
              <center>Saturday</center>
            </b>
          </td>
        </tr>
        <c:set var="d" value="1" />
        <c:set var="d" value="1" />
        <c:forEach var="x" begin="1" end="35">
          <c:if
          test="${(x==1)||(x==8)||(x==15)||(x==22)||(x==29)}">
            </tr><tr>
          </c:if>
          <td>
          <c:if
          test="${ (d<=max) && ((x>7)||(i<=x)) }">
            <c:out value="${d}" />
            <c:set var="d" value="${d+1}" />
          </c:if>
          &#160;
          <br />
          <br />
          <br />
          </td>
        </c:forEach>
      </table>
    </c:if>
  </body>
</html>





Format Locale date in JSP

<%@ taglib uri="http://java.sun.ru/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.ru/jstl/fmt" prefix="fmt" %>
<jsp:useBean id="date" class="java.util.Date" />
<html>
<head><title><fmt:message key="Welcome" /></title></head>
<body>
<h2><fmt:message key="Hello" /> <fmt:message key="and" /> <fmt:message key="Welcome" /></h2>
<fmt:formatDate value="${date}" type="both" dateStyle="full" timeStyle="short" /> <br />
Locale: <c:out value="${pageContext.request.locale.language}" />_<c:out value="${pageContext.request.locale.country}" /> 
</body>
</html>





JSTL Format: Date

<%@ taglib uri="http://java.sun.ru/jstl/core" prefix="c" %><%@ taglib uri="http://java.sun.ru/jstl/core-rt" prefix="c-rt" %><%@ taglib uri="http://java.sun.ru/jstl/fmt" prefix="fmt" %>
<html>
  <head>
    <title>Format Date</title>
  </head>
  <body>
    <c-rt:set var="now" value="<%=new java.util.Date()%>" />
    <table border="1" cellpadding="0" cellspacing="0"
    style="border-collapse: collapse" bordercolor="#111111"
    width="63%" id="AutoNumber2">
      <tr>
        <td width="100%" colspan="2" bgcolor="#0000FF">
          <p align="center">
            <b>
              <font color="#FFFFFF" size="4">Formatting: 
              <fmt:formatDate value="${now}" type="both"
              timeStyle="long" dateStyle="long" />
              </font>
            </b>
          </p>
        </td>
      </tr>
      <tr>
        <td width="51%">formatDate type="time"</td>
        <td width="49%">
          <fmt:formatDate type="time" value="${now}" />
        </td>
      </tr>
      <tr>
        <td width="51%">type="date"</td>
        <td width="49%">
          <fmt:formatDate type="date" value="${now}" />
        </td>
      </tr>
      <tr>
        <td width="51%">type="both"</td>
        <td width="49%">
          <fmt:formatDate type="both" value="${now}" />
        </td>
      </tr>
      <tr>
        <td width="51%">type="both" dateStyle="default"
        timeStyle="default"</td>
        <td width="49%">
          <fmt:formatDate type="both" dateStyle="default"
          timeStyle="default" value="${now}" />
        </td>
      </tr>
      <tr>
        <td width="51%">type="both" dateStyle="short"
        timeStyle="short"</td>
        <td width="49%">
          <fmt:formatDate type="both" dateStyle="short"
          timeStyle="short" value="${now}" />
        </td>
      </tr>
      <tr>
        <td width="51%">type="both" dateStyle="medium"
        timeStyle="medium"</td>
        <td width="49%">
          <fmt:formatDate type="both" dateStyle="medium"
          timeStyle="medium" value="${now}" />
        </td>
      </tr>
      <tr>
        <td width="51%">type="both" dateStyle="long"
        timeStyle="long"</td>
        <td width="49%">
          <fmt:formatDate type="both" dateStyle="long"
          timeStyle="long" value="${now}" />
        </td>
      </tr>
      <tr>
        <td width="51%">type="both" dateStyle="full"
        timeStyle="full"</td>
        <td width="49%">
          <fmt:formatDate type="both" dateStyle="full"
          timeStyle="full" value="${now}" />
        </td>
      </tr>
      <tr>
        <td width="51%">pattern="yyyy-MM-dd"</td>
        <td width="49%">
          <fmt:formatDate pattern="yyyy-MM-dd" value="${now}" />
        </td>
      </tr>
    </table>
  </body>
</html>





JSTL Parse Date

<%@ taglib uri="http://java.sun.ru/jstl/core" prefix="c" %><%@ taglib uri="http://java.sun.ru/jstl/fmt" prefix="fmt" %>
<html>
  <head>
    <title>Parse Date</title>
  </head>
  <body>
    <form method="POST">
      <table border="1" cellpadding="0" cellspacing="0"
      style="border-collapse: collapse" bordercolor="#111111"
      width="62%" id="AutoNumber1">
        <tr>
          <td width="100%" colspan="2" bgcolor="#0000FF">
            <p align="center">
              <b>
                <font color="#FFFFFF" size="4">Date
                Formatting</font>
              </b>
            </p>
          </td>
        </tr>
        <tr>
          <td width="47%">Enter a date to be parsed:</td>
          <td width="53%">
            <input type="text" name="num" size="20" />
          </td>
        </tr>
        <tr>
          <td width="100%" colspan="2">
            <p align="center">
              <input type="submit" value="Submit" name="submit" />
              <input type="reset" value="Reset" name="reset" />
            </p>
          </td>
        </tr>
      </table>
      <p>&#160;</p>
    </form>
    <c:if test="${pageContext.request.method=="POST"}">
      <table border="1" cellpadding="0" cellspacing="0"
      style="border-collapse: collapse" bordercolor="#111111"
      width="63%" id="AutoNumber2">
        <tr>
          <td width="100%" colspan="2" bgcolor="#0000FF">
            <p align="center">
              <b>
                <font color="#FFFFFF" size="4">Formatting: 
                <c:out value="${param.num}"  escapeXml="false" />
                </font>
              </b>
            </p>
          </td>
        </tr>
        <tr>
          <td width="51%">type="date" dateStyle="short"</td>
          <td width="49%">
            <c:catch var="e">
              <fmt:parseDate var="i" type="date" dateStyle="short"
              value="${param.num}" />
              <c:out value="${i}"  escapeXml="false" />
            </c:catch>
            <c:out value="${e}"  escapeXml="false" />
          </td>
        </tr>
        <tr>
          <td width="51%">type="date" dateStyle="medium"</td>
          <td width="49%">
            <c:catch var="e">
              <fmt:parseDate var="i" type="date" dateStyle="medium"
              value="${param.num}" />
              <c:out value="${i}"  escapeXml="false" />
            </c:catch>
            <c:out value="${e}"  escapeXml="false" />
          </td>
        </tr>
        <tr>
          <td width="51%">type="date" dateStyle="long"</td>
          <td width="49%">
            <c:catch var="e">
              <fmt:parseDate var="i" type="date" dateStyle="long"
              value="${param.num}" />
              <c:out value="${i}"  escapeXml="false" />
            </c:catch>
            <c:out value="${e}"  escapeXml="false" />
          </td>
        </tr>
        <tr>
          <td width="51%">type="date" dateStyle="full"</td>
          <td width="49%">
            <c:catch var="e">
              <fmt:parseDate var="i" type="date" dateStyle="full"
              value="${param.num}" />
              <c:out value="${i}"  escapeXml="false" />
            </c:catch>
            <c:out value="${e}"  escapeXml="false" />
          </td>
        </tr>
      </table>
    </c:if>
  </body>
</html>





JSTL Time Zone

<%@ taglib uri="http://java.sun.ru/jstl/core" prefix="c" %><%@ taglib uri="http://java.sun.ru/jstl/core-rt" prefix="c-rt" %><%@ taglib uri="http://java.sun.ru/jstl/fmt" prefix="fmt" %>
<html>
  <head>
    <title>Timezones</title>
  </head>
  <body>
    <c-rt:set var="now" value="<%=new java.util.Date()%>" />
    <table border="1" cellpadding="0" cellspacing="0"
    style="border-collapse: collapse" bordercolor="#111111"
    width="63%" id="AutoNumber2">
      <tr>
        <td width="100%" colspan="2" bgcolor="#0000FF">
          <p align="center">
            <b>
              <font color="#FFFFFF" size="4">Formatting: 
              <fmt:formatDate value="${now}" type="both"
              timeStyle="long" dateStyle="long" />
              </font>
            </b>
          </p>
        </td>
      </tr>
      <c-rt:forEach var="zone"
      items="<%=java.util.TimeZone.getAvailableIDs()%>">
        <tr>
          <td width="51%">
            <c:out value="${zone}" />
          </td>
          <td width="49%">
            <fmt:timeZone value="${zone}">
              <fmt:formatDate value="${now}" timeZone="${zn}"
              type="both" />
            </fmt:timeZone>
          </td>
        </tr>
      </c-rt:forEach>
    </table>
  </body>
</html>