4

JSTLで明日の日付を取得するために、次のことを試みました。

           <c:set var="currDate" value="<%=java.util.Calendar.getInstance()%>"/>
           <fmt:formatDate type="date" value="${currDate.add(java.util.Calendar.DATE,1)}" var="dayEnd"/>

しかし、少なくともvarcurrDateを使用してチェックするために印刷したとき

           <c:out value="${currDate}" />

どうやら、それはうまくいきませんでした。それならどうすればいいですか?

4

3 に答える 3

9

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タグを使用すると、スクリプトレットで行っていたいたずらなことを実行できます。JodaTimeは、常に正確な結果を提供することを信頼できます。

于 2014-07-31T12:17:39.527 に答える
8
<%@ 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"/>
于 2012-12-04T17:11:51.750 に答える
1
<%
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);%>
于 2012-12-03T18:53:19.183 に答える