Java Tutorial/JSTL/Format Date
Содержание
- 1 Format Both Date and Time in Full Style
- 2 Format Both Date and Time in Long Style
- 3 Format Both Date and Time in Medium Style
- 4 Format Both Date and Time in Short Style
- 5 Format Both Date and Time with Default Style Specified
- 6 Format Both Date And Time with Default value
- 7 Format Date with Default Value
- 8 Format Date with Pattern
- 9 Format Date With Timezone
- 10 Format time with default value
Format Both Date and Time in Full Style
<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()%>" /> <fmt:formatDate type="both" dateStyle="full" timeStyle="full" value="${now}" /> </body>
</html></source>
Format Both Date and Time in Long Style
<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()%>" /> <fmt:formatDate type="both" dateStyle="long" timeStyle="long" value="${now}" /> </body>
</html></source>
Format Both Date and Time in Medium Style
<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()%>" /> <fmt:formatDate type="both" dateStyle="medium" timeStyle="medium" value="${now}" /> </body>
</html></source>
Format Both Date and Time in Short Style
<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()%>" /> <fmt:formatDate type="both" dateStyle="short" timeStyle="short" value="${now}" /> </body>
</html></source>
Format Both Date and Time with Default Style Specified
<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()%>" /> <fmt:formatDate type="both" dateStyle="default" timeStyle="default" value="${now}" /> </body>
</html></source>
Format Both Date And Time with Default value
<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()%>" /> <fmt:formatDate type="both" value="${now}" /> </body>
</html></source>
Format Date with Default Value
<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()%>" /> <fmt:formatDate type="date" value="${now}" /> </body>
</html></source>
Format Date with Pattern
<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()%>" /> <fmt:formatDate pattern="yyyy-MM-dd" value="${now}" /> </body>
</html></source>
Format Date With Timezone
<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()%>" />
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>
Format time with default value
<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()%>" /> <fmt:formatDate type="time" value="${now}" /> </body>
</html></source>