Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

get date of tomorrow using jstl

Tags:

jsp

jstl

calendar

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?

like image 885
Doni Andri Cahyono Avatar asked Dec 01 '22 20:12

Doni Andri Cahyono


2 Answers

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.

like image 78
mwarren Avatar answered Jan 21 '23 08:01

mwarren


<%@ 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"/>
like image 23
rickz Avatar answered Jan 21 '23 10:01

rickz