我尝试了以下方法来获取JSTL中的明天的日期:
<c:set var="currDate" value="<%=java.util.Calendar.getInstance()%>"/>
<fmt:formatDate type="date" value="${currDate.add(java.util.Calendar.DATE,1)}" var="dayEnd"/>但是当我打印出来检查至少var currDate时,使用
<c:out value="${currDate}" />显然,它没有起作用。那我该怎么办呢?
发布于 2012-12-05 01:11:52
<%@ 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"/>发布于 2014-07-31 20:17:40
在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"/>但是,例如,当时钟更改为夏令时时,您不能依赖java.util.Date。
因此,如果您真的想用JSTL完成所有这些工作,您可以使用Spring标记、Joda Time类和Joda标记。
<jsp:useBean id="ourDate" class="org.joda.time.DateTime"/>
<spring:eval expression="ourDate.plusDays(1)" var="tomorrow"/>
tomorrow: <joda:format value="${tomorrow}" style="SM" />spring eval标签可以让你做一些我们过去经常用scriptlet做的事情,而且我们可以相信JodaTime总是能给你一个准确的结果。
发布于 2012-12-04 02:53:19
<%
Calendar now=Calendar.getInstance();
Calendar today=Calendar.getInstance();
Calendar calendar=Calendar.getInstance();
today.set(today.get(Calendar.YEAR),today.get(Calendar.MONTH), today.get(Calendar.DAY_OF_MONTH));
// Convert the date string into a date to manipulate the dates
String year = (String)pageContext.getAttribute("year");
String month = (String)pageContext.getAttribute("month");
String day = (String)pageContext.getAttribute("day");
if (year == null) {
year = String.format("%4d",now.get(Calendar.YEAR));
int mth = now.get(Calendar.MONTH) + 1;
month = String.format("%02d",mth);
day = String.format("%02d",now.get(Calendar.DAY_OF_MONTH));
}
String str_date = month + "/" + day + "/" + year;
SimpleDateFormat format = new SimpleDateFormat("MM/dd/yyyy");
Date date = (Date)format.parse(str_date);
calendar.setTime(date);
Date [] arrayOfDates = new Date[4];
arrayOfDates[0] = calendar.getTime();
pageContext.setAttribute("calendarDate",arrayOfDates);
pageContext.setAttribute("year",year);
pageContext.setAttribute("month",month);
pageContext.setAttribute("day",day);%>https://stackoverflow.com/questions/13661212
复制相似问题