[英]Freemarker/Velocity - date manipulation
I have a fairly simple question about these 2 templating engines. 关于这两个模板引擎,我有一个非常简单的问题。
I'm trying to make a future/past date, a fixed time from now, eg 18 months ago, or tomorrow. 我正在尝试设定一个将来/过去的日期,从现在开始的固定时间,例如18个月前或明天。
I know that it is possible to do this with a java date object inside a velocity/freemarker template (something like $date.add(2,-18)
), but I would like to do this with DateTool or freemarker core. 我知道可以用速度/ freemarker模板(类似于
$date.add(2,-18)
)中的java date对象来执行此操作,但我想使用DateTool或freemarker核心来执行此操作 。
This is something that I see as purely presentational (just think at the default dates you see in flight booking forms), so I can't see any reason why a templating engine shouldn't be able to do this. 我认为这纯粹是演示性的(只是考虑在预订表中看到的默认日期),因此我看不到任何原因导致模板引擎无法执行此操作。
Is it possible though? 有可能吗? If so, how?
如果是这样,怎么办?
I know this is post is really old, but for people from the future still looking for an answer: Date manipulation can be done converting dates to/from milliseconds: 我知道这是真的很老的帖子,但是对于将来的人们来说,仍然在寻找答案:可以通过将日期转换为毫秒来进行日期操作:
${(mydate?long - 18 * 86400000)?number_to_date?string("yyyy-MM-dd")}
for example would subtract 18 days from mydate
. 例如将从
mydate
减去18天。 (Note that 86400000 is the amount of milliseconds per day) (请注意,86400000是每天的毫秒数)
对于freemarker,也许是: 内置日期 。
在Velocity Tools中,没有这种方法。
You can do it in Velocity too, but not with the DateTool (that belongs the Velocity Extras anyway). 您也可以在Velocity中完成此操作,但不能使用DateTool(总之属于Velocity Extras)完成此操作。
It is a good practice to have a "Format" object with various utilities that are practical for presentational purposes only (you can see the various frameworks that use Velocity how they have "Format" objects). 好的做法是让“格式”对象带有各种实用工具,这些实用工具仅用于演示目的(您可以看到使用Velocity的各种框架如何使用“格式”对象)。
So your code would look like $format.dateFromNow(), and there would be still a presentational approach. 因此,您的代码将类似于$ format.dateFromNow(),并且仍然会有一种表示方法。
I found that per @Stefan Haberl, ?long
does work on dates to get the same value as java.util.Date.getTime()
as a Long
. 我发现每个@Stefan Haberl 都可以在日期上使用
?long
来获取与 Long
相同的值与java.util.Date.getTime()
。 However, I needed a little more explanation to be able to compare dates or modify dates. 但是,我需要更多说明才能比较日期或修改日期。
Here's the gist: 这是要点:
?long
= java.util.Date.getTime()
returns epoch time in milliseconds ?long
= java.util.Date.getTime()
返回纪元时间(以毫秒为单位) I like working in seconds instead of milliseconds (less unnecessary zeros, I don't care about milliseconds, etc.), which looks like this: 我喜欢用秒而不是毫秒来工作(减少不必要的零,我不在乎毫秒等),如下所示:
[#function convertToUnix date]
[#return (date?date?long / 1000)]
[/#function]
[#-- Output Unix Timestamp --]
${convertToUnix(.now)}
At this point, 86400
= 1 day (because we are in "seconds" now), so you can simply add/subtract that to manipulate the date. 此时,
86400
= 1天(因为我们现在处于“秒”状态),因此您只需添加/减去该日期即可。
[#assign
day = 86400
week = 7 * day
avgMonth = 365.25 / 12 * day
testingEndOfDay = convertToUnix(.now) < (convertToUnix(sameDay) + day)
testingYesterday = convertToUnix(.now) < (convertToUnix(yesterday) + day)
]
${testingEndOfDay?c} # true, .now is less than the end of the day
${testingYesterday?c} # false, .now is greater than the end of yesterday
Note : I am ignoring the time of day, we received dates that started at 12:00AM and wanted to check against
.now
for the end of the day.注意 :我忽略了一天中的时间,我们收到的日期开始于12:00 AM,并希望在当天结束时检查
.now
。
Now, if I want to get a date back from the Unix format (in seconds), I can convert it back using the ?number_to_date
builtin 现在,如果我想从Unix格式中获取日期(以秒为单位),则可以使用内置的
?number_to_date
来将其转换回
[#assign
nowAsUnix = convertToUnix(.now)
prettyDate = (nowAsUnix * 1000)?number_to_date
]
Note : I'm open to edits/improvements as I'm not sure why much of this was required ¯\\_(ツ)_/¯ 注意 :我可以进行编辑/改进,因为我不确定为什么需要这么多¯\\ _(ツ)_ /¯
You can write your own methods to use in FreeMarker: http://freemarker.sourceforge.net/docs/pgui_datamodel_method.html 您可以编写自己的方法在FreeMarker中使用: http : //freemarker.sourceforge.net/docs/pgui_datamodel_method.html
build a DataAddMethod that executes this logic. 构建执行此逻辑的DataAddMethod。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.