Java Tutorial/JSTL/Parse Date

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

Output All TimeZone

<%@ 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" width="63%" id="AutoNumber2">
      <tr>
        <td width="100%" colspan="2">
        Formatting: <fmt:formatDate value="${now}" type="both" timeStyle="long" dateStyle="long" />
        </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>





Parse date from form

<%@ 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>
        <tr>
          <td width="100%" colspan="2">
            Date Formatting
          </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>
        <tr>
          <td width="100%" colspan="2">
            Formatting: <c:out value="${param.num}"  escapeXml="false" />
          </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>





Parse date input from form and format

<%@ taglib prefix="c" uri="http://java.sun.ru/jstl/core" %>
<form method="post" action="dateHandler.jsp">
  Please enter your birthday:
  <select name="month">
    <option value="Jan">January</option>
    <option value="Feb">February</option>
    <option value="Mar">March</option>
    <option value="Apr">April</option>
    <option value="May">May</option>
    <option value="Jun">June</option>
    <option value="Jul">July</option>
    <option value="Aug">August</option>
    <option value="Sep">September</option>
    <option value="Oct">October</option>
    <option value="Nov">November</option>
    <option value="Dec">December</option>
  </select>
  <select name="day">
    <c:forEach begin="1" end="31" var="day">
      <option><c:out value="${day}"/></option>
    </c:forEach>
  </select>
  <select name="year">
    <c:forEach begin="1930" end="2003" var="year">
      <option><c:out value="${year}"/></option>
    </c:forEach>
  </select>
  <input type="submit" value="Submit" />
</form>