Adding Days and Comparing Dates
Date Operations#
Today's Date#
Here are a few helpful ways to get today's date in velocity and return the value as a string:
Today's date is:
,[object Object],${esc.d}date.format('yyyyMMdd',${esc.d}date) = $date.format('yyyyMMdd',$date)
,[object Object],${esc.d}date = $date
,[object Object],${esc.d}date.long = $date.long
,[object Object],${esc.d}date.medium_time = $date.medium_time
,[object Object],${esc.d}date.full_date = $date.full_date
,[object Object],${esc.d}date.get('default','short') = $date.get('default','short')
,[object Object],${esc.d}date.get('yyyy-M-d H:m:s') = $date.get('yyyy-M-d H:m:s')
Adding/Subtracting Dates#
This code gets a java.util.Calendar and uses it to calculate 24 hours from now:
My date: ${esc.d}date
,[object Object],
,[object Object],${esc.h}${esc.h} Get a java Calendar
,[object Object],${esc.h}set(${esc.d}x = ${esc.d}date.calendar)
,[object Object],
,[object Object],${esc.h}${esc.h} Add 24 hours (hours=int code 10 - see list below)
,[object Object],${esc.d}x.add(10,24)
,[object Object],
,[object Object],${esc.h}${esc.h} Show result
,[object Object],My new date :${esc.d}x.time,[object Object],[object Object],
Adding and Setting dates#
You can also set dates and time with the calendar object. For example, to get a date at midnight one year ago:
My date: ${esc.d}date
,[object Object],
,[object Object],${esc.h}${esc.h} Get a java Calendar
,[object Object],${esc.h}set(${esc.d}x = ${esc.d}date.calendar)
,[object Object],
,[object Object],${esc.h}${esc.h} Subtract 1 year (year=int code 1 - see list below)
,[object Object],${esc.d}x.add(1,-1),[object Object],[object Object],
,[object Object],${esc.h}${esc.h} Set the hours, minutes and seconds to 0
,[object Object],${esc.d}x.set(10,0) ${esc.h}${esc.h} hours
,[object Object],${esc.d}x.set(12,0) ${esc.h}${esc.h} minutes
,[object Object],${esc.d}x.set(13,0) ${esc.h}${esc.h} seconds
,[object Object],${esc.h}${esc.h} Show result in SQL format -
,[object Object],${esc.d}date.format('yyyy-MM-dd HH:mm:ss',${esc.d}x.time)
Constants#
Here are a list of the constants available in the java calendar that you can use to add or subtract or set (we used 10 in the example above). All constants are declared as public static final int
.
Comparison#
To compare date strings they first must be converted to a Calendar and then use the compareTo method:
${esc.h}${esc.h} Converting string date to Calendar
,[object Object],${esc.h}set(${esc.d}nowDate=${esc.d}date.toCalendar(${esc.d}date))
,[object Object],
,[object Object],${esc.h}${esc.h} We compare to today's date
,[object Object],${esc.h}if(${esc.d}calDate.compareTo(${esc.d}date)==0)
,[object Object],Publish Date is today
,[object Object],
,[object Object],${esc.h}elseif(${esc.d}calDate.compareTo(${esc.d}date)>0)
,[object Object],Publish Date is in the future
,[object Object],
,[object Object],${esc.h}else
,[object Object],Publish Date is in the past
,[object Object],${esc.h}end
The examples above are put together here:
${esc.h}${esc.h} Add Days to a Date: Sets a start Date 7 days before today.
,[object Object],(You can use a positive or negative number of days days.)
,[object Object],${esc.h}set(${esc.d}startDate = ${esc.d}date.format('yyyyMMdd', ${esc.d}UtilMethods.addDays(${esc.d}date.date, -7)))
,[object Object],<p>This is the start date string: ${esc.d}startDate</p>
,[object Object],
,[object Object],${esc.h}${esc.h} This returns a string with the numbers in today's date
,[object Object],${esc.h}${esc.h} Example: "20120823" (Aug 23, 2012)
,[object Object],${esc.h}set(${esc.d}today = ${esc.d}date.format('yyyyMMdd',${esc.d}date))
,[object Object],
,[object Object],<p>This is today's date string: ${esc.d}today</p>
,[object Object],
,[object Object],${esc.h}${esc.h} Converting string date to Calendar
,[object Object],${esc.h}set(${esc.d}calDate=${esc.d}date.toCalendar(${esc.d}con.publishedDate))
,[object Object],${esc.h}set(${esc.d}nowDate=${esc.d}date.toCalendar(${esc.d}date))
,[object Object],
,[object Object],${esc.h}${esc.h} We compare to today's date
,[object Object],${esc.h}if(${esc.d}calDate.compareTo(${esc.d}nowDate)==0)
,[object Object],Publish Date is today
,[object Object],
,[object Object],${esc.h}elseif(${esc.d}calDate.compareTo(${esc.d}nowDate)>0)
,[object Object],Publish Date is in the future
,[object Object],
,[object Object],${esc.h}else
,[object Object],Publish Date is in the past
,[object Object],${esc.h}end