簡體   English   中英

減去日期並恢復速度

[英]subtract a date and recovering and velocity

在搜索過程中,我想了解一些有關我的問題的信息。

我想減去當前日期的三個月,因此通常是:

##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))

但是我什么都沒有,請給我個建議嗎?

啤酒。

首要建議是不要自己編寫語法。 :) 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

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及其相關問題…

// 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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM