I tried the following to get the date of tomorrow in JSTL:
<c:set var="currDate" value="<%=java.util.Calendar.getInstance()%>"/>
<fmt:formatDate type="date" value="${currDate.add(java.util.Calendar.DATE,1)}" var="dayEnd"/>
but when I printed out to check at least var currDate using
<c:out value="${currDate}" />
apparently, it didn't work. What should i do then?
There is neater way to do this directly in JSTL
<jsp:useBean id="ourDate" class="java.util.Date"/>
<jsp:setProperty name="ourDate" property="time" value="${ourDate.time + 86400000}"/>
<fmt:formatDate value="${ourDate}" pattern="dd/MM/yyyy"/>
But you can't rely on java.util.Date when the clocks change to Summer Time for example.
So if you REALLY want to do this all in JSTL, you can, with Spring tags, Joda Time classes and Joda Tags.
<jsp:useBean id="ourDate" class="org.joda.time.DateTime"/>
<spring:eval expression="ourDate.plusDays(1)" var="tomorrow"/>
tomorrow: <joda:format value="${tomorrow}" style="SM" />
The spring eval tag lets you do some of those naughty things we all used to do with scriptlets, and JodaTime can be trusted to always give you an accurate result.
<%@ page import="java.util.Date" %>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<c:set var="today" value="<%=new Date()%>"/>
<c:set var="tomorrow" value="<%=new Date(new Date().getTime() + 60*60*24*1000)%>"/>
Today: <fmt:formatDate type="date" value="${today}" pattern="d"/>
Tomorrow: <fmt:formatDate type="date" value="${tomorrow}" pattern="d"/>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With