在日期中添加时间跨度?

发布于 2024-12-02 18:51:51 字数 228 浏览 0 评论 0原文

谁能想到一种为日期添加时间跨度的有效方法?

类似于以下内容:

    <cfset foo = now() + createTimeSpan(15,12,30,30)>

基于 .NET 的 CFML 引擎中的 IIRC 我可以简单地使用 date.add(timespan),但我现在不记得等效的 Java 快捷方式。

提前致谢。

Can anyone think of an efficient way of adding a timespan to a date?

Something like the following:

    <cfset foo = now() + createTimeSpan(15,12,30,30)>

IIRC in a .NET-based CFML engine I could simply use date.add(timespan), but I can't remember the equivalent Java shortcut right now.

Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

夜司空 2024-12-09 18:51:51

您实际上在这里问什么?一方面,您将其标记为 CF 问题并使用 CFML 来回答您自己的问题;然后你开始询问 Java 捷径?

如果您想知道如何在 CFML 中执行此操作,那么您的代码示例就是您如何在 CFML 中执行此操作。

如果你想知道如何将 CF 时间跨度值(这只是天数的数字表示)添加到 Java 日期,那么它似乎有点棘手,因为我可以找到的 Calendar 方法都添加了组成部分的时间跨度,而不是整个时间跨度。

这段代码可能演示了您想要的所有答案(除了如何使用 Java 日期/日历一次性完成它):

<cfset dTs = now()><!--- grab now --->
<cfset fTimespan = createTimeSpan(15,12,30,30)><!--- make a timespan --->
<cfset fLater = dTs + fTimespan><!--- add the timespan to now yields a float --->
<cfset sLater = dateFormat(fLater, "yyy-mm-dd") & " " & timeformat(fLater, "HH:MM:SS")><!--- but you can treat a float as a date/time --->
<cfset dLater = createOdbcDateTime(fLater)><!--- or convert it back to a date object --->

<cfset jCal = createObject("java", "java.util.GregorianCalendar").init()><!--- java.util.Date is basically deprecated in favour of calendars --->
<cfset jCal.add(jCal.DAY_OF_MONTH, 15)><!--- one needs to set each part of the timespan separately --->
<cfset jCal.add(jCal.HOUR_OF_DAY, 12)>
<cfset jCal.add(jCal.MINUTE, 30)>
<cfset jCal.add(jCal.SECOND, 30)>
<cfset sJCal = jCal.getTime()><!--- this gets a string that CF can use as a date back out of the calendar --->
<cfset bIsDate = isDate(sJCal)><!--- demonstrate that last statement to be true --->
<cfdump var="#variables#"><!--- and all the results --->

这是否回答了您实际的问题?

What actually are you asking here? On one hand you've tagged it as a CF question and use CFML which answers your own question; then you start asking about a Java short cut?

If you want to know how to do it in CFML, then your code sample is how you do it in CFML.

If you want to know how to add a CF timespan value (which is just a numeric representation of a number of days) to a Java date, then it seems to be slightly trickier, because the Calendar methods I can find all add the component parts of the timespan, not the whole timespan.

This code demonstrates possibly all the answers you're after (except how to do it in one hit with a Java date/calendar):

<cfset dTs = now()><!--- grab now --->
<cfset fTimespan = createTimeSpan(15,12,30,30)><!--- make a timespan --->
<cfset fLater = dTs + fTimespan><!--- add the timespan to now yields a float --->
<cfset sLater = dateFormat(fLater, "yyy-mm-dd") & " " & timeformat(fLater, "HH:MM:SS")><!--- but you can treat a float as a date/time --->
<cfset dLater = createOdbcDateTime(fLater)><!--- or convert it back to a date object --->

<cfset jCal = createObject("java", "java.util.GregorianCalendar").init()><!--- java.util.Date is basically deprecated in favour of calendars --->
<cfset jCal.add(jCal.DAY_OF_MONTH, 15)><!--- one needs to set each part of the timespan separately --->
<cfset jCal.add(jCal.HOUR_OF_DAY, 12)>
<cfset jCal.add(jCal.MINUTE, 30)>
<cfset jCal.add(jCal.SECOND, 30)>
<cfset sJCal = jCal.getTime()><!--- this gets a string that CF can use as a date back out of the calendar --->
<cfset bIsDate = isDate(sJCal)><!--- demonstrate that last statement to be true --->
<cfdump var="#variables#"><!--- and all the results --->

Does that answer whatever your question actually was?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文