Java Tutorial/JSTL/Format Date

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

Format Both Date and Time in Full Style

<%@ 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()%>" />
    <fmt:formatDate type="both" dateStyle="full" timeStyle="full" value="${now}" />
  </body>
</html>





Format Both Date and Time in Long Style

<%@ 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()%>" />
    <fmt:formatDate type="both" dateStyle="long" timeStyle="long" value="${now}" />
  </body>
</html>





Format Both Date and Time in Medium Style

<%@ 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()%>" />
    <fmt:formatDate type="both" dateStyle="medium" timeStyle="medium" value="${now}" />
  </body>
</html>





Format Both Date and Time in Short Style

<%@ 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()%>" />
    <fmt:formatDate type="both" dateStyle="short" timeStyle="short" value="${now}" />
  </body>
</html>





Format Both Date and Time with Default Style Specified

<%@ 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()%>" />
    <fmt:formatDate type="both" dateStyle="default" timeStyle="default" value="${now}" />
  </body>
</html>





Format Both Date And Time with Default value

<%@ 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()%>" />
                <fmt:formatDate type="both" value="${now}" />
  </body>
</html>





Format Date with Default Value

<%@ 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()%>" />
                <fmt:formatDate type="date" value="${now}" />
  </body>
</html>





Format Date with Pattern

<%@ 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()%>" />
    <fmt:formatDate pattern="yyyy-MM-dd" value="${now}" />
  </body>
</html>





Format Date With 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>





Format time with default value

<%@ 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()%>" />
                <fmt:formatDate type="time" value="${now}" />
  </body>
</html>