1

在我的搜索过程中,我想了解一些关于我的问题的信息。

我想在当前日期减去三个月,通常是:

##set($user.end = $date.format("yyyy-MM-dd", $date.date)) 
##set($user.begin = "${user.end}-03") 

但我什么也没恢复。

我尝试:

##set($R = $date.format("yyyy-MM", $date.date))
##set($query.end = $R)
##set($user.begin = "${R}-03")
#set($query.end = $date.format("yyyy-MM", $date.date))

但是我什么都没有,请问您有什么建议可以给我吗?

啤酒。

4

2 回答 2

1

第一个建议是不要为事物编写自己的语法。:) 在 Java 或 Velocity 中都没有日期减法。您必须自己设置值。通过自定义工具在 Java 或 Velocity 中执行此操作可能会更好,但无论如何它都在 VTL 中......

#set( $user.end = $date.format("yyyy-MM", $date.date) )
#set( $begin = $date.date.clone() )
#set( $month = $begin.month - 3 )
#if( $month < 0 )
  #set( $month = $month + 12 )
  #set( $begin.year = $begin.year - 1)
#end
#set( $begin.month = $month )
#set( $user.begin = $date.format("yyyy-MM", $begin) )

当然,这只是通过 Velocity 使用 java.util.Date API。 http://docs.oracle.com/javase/7/docs/api/java/util/Date.html

于 2013-11-08T16:46:32.903 回答
0

在Joda-Time中使用日期时间进行这种数学运算更容易。

在 Java 7 中使用 Joda-Time 2.3 的示例源代码:

// © 2013 Basil Bourque. This source code may be used freely forever by anyone taking full responsibility for doing so.

org.joda.time.DateTimeZone losAngelesTimeZone = org.joda.time.DateTimeZone.forID("America/Los_Angeles");
org.joda.time.DateTime november15 = new org.joda.time.DateTime(2013, 11, 15, 18, 0, losAngelesTimeZone);
org.joda.time.DateTime threeMonthsPrior = november15.minusMonths(3);

System.out.println("november15: " + november15);
System.out.println("threeMonthsPrior: " + threeMonthsPrior);

运行时...(请注意,我们甚至超过了夏令时 (DST),-8 与 -7)

november15: 2013-11-15T18:00:00.000-08:00
threeMonthsPrior: 2013-08-15T18:00:00.000-07:00

关于 Joda-Time 及相关问题……</p>

// Joda-Time - The popular alternative to Sun/Oracle's notoriously bad date, time, and calendar classes bundled with Java 7 and earlier.
// http://www.joda.org/joda-time/

// Joda-Time will become outmoded by the JSR 310 Date and Time API introduced in Java 8.
// JSR 310 was inspired by Joda-Time but is not directly based on it.
// http://jcp.org/en/jsr/detail?id=310

// By default, Joda-Time produces strings in the standard ISO 8601 format.
// https://en.wikipedia.org/wiki/ISO_8601

// About Daylight Saving Time (DST): https://en.wikipedia.org/wiki/Daylight_saving_time

// Time Zone list: http://joda-time.sourceforge.net/timezones.html
于 2013-11-08T23:35:34.913 回答