Java/JSTL/Date

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

Date Formating in JSTL

   <source lang="java">

<%@ 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">

Date Formatting

Enter a month(1-12)
           <input type="text" name="month" size="20" />
Enter a year(i.e. 2002)
           <input type="text" name="year" size="20" />

<input type="submit" value="Submit" name="submit" /> <input type="reset" value="Reset" name="reset" />

 

   </form>
   <c:if test="${pageContext.request.method=="POST"}">
<fmt:parseDate var="now" value="${param.month}/1/${param.year}" type="date" dateStyle="short" /> <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> <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> </c:if> </c:forEach>

<fmt:formatDate pattern="MMMMM yyyy" value="${now}" />

           
Sunday
           
           
Monday
           
           
Tuesday
           
           
Wednesday
           
           
Thursday
           
           
Friday
           
           
Saturday
           
         <c:if
         test="${ (d<=max) && ((x>7)||(i<=x)) }">
           <c:out value="${d}" />
           <c:set var="d" value="${d+1}" />
         </c:if>
          
         


   </c:if>
 </body>

</html>


      </source>
   
  
 
  



Format Locale date in JSP

   <source lang="java">

<%@ 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>

<fmt:message key="Hello" /> <fmt:message key="and" /> <fmt:message key="Welcome" />

<fmt:formatDate value="${date}" type="both" dateStyle="full" timeStyle="short" />
Locale: <c:out value="${pageContext.request.locale.language}" />_<c:out value="${pageContext.request.locale.country}" /> </body> </html>


      </source>
   
  
 
  



JSTL Format: Date

   <source lang="java">

<%@ 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()%>" />

Formatting: <fmt:formatDate value="${now}" type="both" timeStyle="long" dateStyle="long" />

formatDate type="time"
         <fmt:formatDate type="time" value="${now}" />
type="date"
         <fmt:formatDate type="date" value="${now}" />
type="both"
         <fmt:formatDate type="both" value="${now}" />
type="both" dateStyle="default" timeStyle="default"
         <fmt:formatDate type="both" dateStyle="default"
         timeStyle="default" value="${now}" />
type="both" dateStyle="short" timeStyle="short"
         <fmt:formatDate type="both" dateStyle="short"
         timeStyle="short" value="${now}" />
type="both" dateStyle="medium" timeStyle="medium"
         <fmt:formatDate type="both" dateStyle="medium"
         timeStyle="medium" value="${now}" />
type="both" dateStyle="long" timeStyle="long"
         <fmt:formatDate type="both" dateStyle="long"
         timeStyle="long" value="${now}" />
type="both" dateStyle="full" timeStyle="full"
         <fmt:formatDate type="both" dateStyle="full"
         timeStyle="full" value="${now}" />
pattern="yyyy-MM-dd"
         <fmt:formatDate pattern="yyyy-MM-dd" value="${now}" />
 </body>

</html>


      </source>
   
  
 
  



JSTL Parse Date

   <source lang="java">

<%@ 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">

Date Formatting

Enter a date to be parsed:
           <input type="text" name="num" size="20" />

<input type="submit" value="Submit" name="submit" /> <input type="reset" value="Reset" name="reset" />

 

   </form>
   <c:if test="${pageContext.request.method=="POST"}">

Formatting: <c:out value="${param.num}" escapeXml="false" />

type="date" dateStyle="short"
           <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" />
type="date" dateStyle="medium"
           <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" />
type="date" dateStyle="long"
           <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" />
type="date" dateStyle="full"
           <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" />
   </c:if>
 </body>

</html>


      </source>
   
  
 
  



JSTL Time Zone

   <source lang="java">

<%@ 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()%>" />
<c-rt:forEach var="zone" items="<%=java.util.TimeZone.getAvailableIDs()%>"> </c-rt:forEach>

Formatting: <fmt:formatDate value="${now}" type="both" timeStyle="long" dateStyle="long" />

           <c:out value="${zone}" />
           <fmt:timeZone value="${zone}">
             <fmt:formatDate value="${now}" timeZone="${zn}"
             type="both" />
           </fmt:timeZone>
 </body>

</html>


      </source>