Freemarker print date in template - templates

I am trying to print the current date when the template is activated. I have read that I have to pass a new Date() Java object to the template, but I don't know how to do that or where to put it in the code.
Does someone know how to pass a Java object to the template in this case?
Thank you !!

Actually you don't have to pass a new Date() to your template, because placing a timestamp into a template's output is quite common and therefore FreeMarker provides a special variable called .now which returns the current date and time. You can use it in your template like this:
Page generated: ${.now}
(FreeMarker also contains different built-ins for formatting dates: http://freemarker.org/docs/ref_builtins_date.html)
Update: Works only with the latest version of FreeMarker, 2.3.17.

${.now} is the perfect answer. Just wanted to add few other ways to get direct values from date
#-- Predefined format names: -->
${openingTime?string.short}
${openingTime?string.medium}
${openingTime?string.long}
${openingTime?string.full}
${openingTime?string.xs} <#-- XSD xs:time -->
${openingTime?string.iso} <#-- ISO 8601 time -->
${.now?string.short}
${.now?string.medium}
${.now?string.long}
${.now?string.full}
${.now?string.xs} <#-- XSD xs:date -->
${.now?string.iso} <#-- ISO 8601 date -->
${.now?string.short}
${.now?string.medium}
${.now?string.long}
${.now?string.full}
${.now?string.medium_short} <#-- medium date, short time -->
${.now?string.xs} <#-- XSD xs:dateTime -->
${.now?string.iso} <#-- ISO 8601 combined date and time -->
<#-- Programmer-defined named format (# + name): -->
${.now?string.#fileDate}
<#-- Advanced ISO 8601 and XSD formatting: -->
${.now?string.iso_m_u}
${.now?string.xs_ms_nz}
<#-- SimpleDateFormat patterns: -->
${.now?string["dd.MM.yyyy, HH:mm"]}
${.now?string["EEEE, MMMM dd, yyyy, hh:mm a '('zzz')'"]}
${.now?string["EEE, MMM d, ''yy"]}
${.now?string.yyyy} <#-- Same as ${.now?string["yyyy"]} -->
will output
01:45 PM
01:45:09 PM
01:45:09 PM PST
01:45:09 PM PST
13:45:09-08:00
13:45:09-08:00
2/20/07
Apr 20, 2007
April 20, 2007
Friday, April 20, 2007
2007-02-20-08:00
2007-02-20
2/20/07 01:45 PM
Feb 20, 2007 01:45:09 PM
February 20, 2007 01:45:09 PM PST
Friday, February 20, 2007 01:45:09 PM PST
Feb 8, 2003 9:24 PM
2007-02-20T13:45:09-08:00
2007-02-20T13:45:09-08:00
Apr/20/2007 13:45
2007-02-20T21:45Z
2007-02-20T13:45:09.000
08.04.2003 21:24
Tuesday, April 08, 2003, 09:24 PM (PDT)
Tue, Apr 8, '03
2003
Source

Use the ObjectConstructor API of Freemarker to create a calendar object and a formatter object, then combine the two to print the date:
<#-- Create constructor object -->
<#assign objectConstructor = "freemarker.template.utility.ObjectConstructor"?new()>
<#-- Call calendar constructor -->
<#assign clock = objectConstructor("java.util.GregorianCalendar")>
<#-- Call formatter constructor -->
<#assign mmddyy = objectConstructor("java.text.SimpleDateFormat","MM/dd/yyyy")>
<#-- Call getTime method to return the date in milliseconds-->
<#assign date = clock.getTime()>
<#-- Call format method to pretty print the date -->
<#assign now = mmddyy.format(date)>
<#-- Display date -->
${now}
The ?new built-in, as it was implemented, was a security hole. Now, it only allows you to instantiate a java object that implements the freemarker.template.TemplateModel interface. If you want the functionality of the ?new built-in as it existed in prior versions, make available an instance of the freemarker.template.utility.ObjectConstructor class to your template. For example:
myDataModel.put("objConstructor", new ObjectConstructor());
and then in the template you can do this:
<#assign aList = objConstructor("java.util.ArrayList", 100)>)
References
Freemarker 2.3: Version History
Tag Developers Guide: Freemarker
CRUD Operations using Servlet and FreeMarker Template Engine

Related

Get datetime using qmake

We can get datetime using qmake _DATE_ which outputs
Sat Mar 12 17:29:00 2022
Can we format this output?
By using QDateTime by passing the value of DATE (in fromString), you may be able to format the output the way you want, by using toString.
You can try things like this :
toString("ddd MMMM d hh:mm:ss yy"); //Sat Mar 12 17:29:00 2022

regex to find the data

EDIT - I added all the last 50 texts, I saw that were sent from various people, unfortunately, it's not an automatic email...
list of all the text is HERE
I'm struggling to find a matched pattern that will identify the needed items (date, start time, time zone) from this text:
1 April 20 16:00-16:30 Israel Time
Tomorrow, Wed Feb 12, 08:00-9:00 AM IST(IL)
Tomorrow, Wed Jan 22, 09:30-10:00 PM PST
11-May-20 19:00-20:30 Israel Time
The start time is an easy one: (\d+:\d+)- but I'm not sure what to be done with the other words and digits.
Based on the data you provided, something like this would do it, with 3 captures as requested:
(\d+[-\s]\w+[-\s]\d+|\w+ \d+),?\s(\d+\:\d+)\-\d+\:\d+\s(?:AM\s|PM\s)?(.*)
Online reference

Regex - Slice Date - Aug 22, 2017 02:00 PM EDT

I'm trying to take a date, for example Aug 22, 2017 02:00 PM EDT
and get the month, day, year from it.
month = re.findall(r'', date)[0]
day = re.findall(r'', date)[0]
year = re.findall(r'', date)[0]
I've started with something like this:
(.*)(?<=[a-zA-Z]{3}\s)
for the month. Is there a better way to do this?
You need to first convert to datetime and then extract the needed values like this (reusing the example):
from datetime import datetime
datetime_object = datetime.strptime('Jun 1 2005 1:33PM', '%b %d %Y %I:%M%p')
print(datetime_object.year, datetime_object.month, datetime_object.day)
From what I can see you probably won't need to specify the format but pass the string directly to the datetime.strptime function.

All CSV values in column 0 are strings

For some reason a csv file I wrote (win7) with Python has all the values as a string in column 0 and cannot perform any operation.
It has no labels.
The format is (I would like to keep the last value - date - as a date format):
"Rob,Avanti,Ave,12.83,Max,4.0,Min,-21.9,analist disp:,-1.0,"" date: Feb 04, 2016 """
EDIT - When I read it with the csv module it prints it out like:
['Rob,Avanti,Ave,12.83,Max,4.0,Min,-21.9,analist disp:,-1.0," date: Feb 04, 2016\t\t\t"']
What is the best way to convert the strings into comma separated values like this?
Rob,Avanti,Ave,12.83,Max,4.0,Min,-21.9,analist disp:,-1.0, date:, Feb 04, 2016
Thanks a lot.
s="Rob,Avanti,Ave,12.83,Max,4.0,Min,-21.9,analist disp:,-1.0,"" date: Feb 04, 2016 """
print(s)
Rob,Avanti,Ave,12.83,Max,4.0,Min,-21.9,analist disp:,-1.0, date: Feb 04, 2016
to add a comma after "date:" you need to add some logic (like replace ":" with ":,"; or after first word etc.
First, your date field is quoted, which is ok (and needed) because there is a comma inside:
" date: Feb 04, 2016 "
But then the whole line also gets quoted (and thus seen as a single field). And because there are already quotes around the date field, those get escaped with another quote:
"Rob,Avanti,Ave,12.83,Max,4.0,Min,-21.9,analist disp:,-1.0,"" date: Feb 04, 2016 """
So, if you remove that last quoting, everything should be fine (but you might want to trim the date field):
Rob,Avanti,Ave,12.83,Max,4.0,Min,-21.9,analist disp:,-1.0," date: Feb 04, 2016 "
If you want it exactly like this, you need another comma after date: :
Rob,Avanti,Ave,12.83,Max,4.0,Min,-21.9,analist disp:,-1.0, date:,"Feb 04, 2016"
On the other hand, it would be better to use a header instead:
Name,Name2,Ave,Max,Min,analist disp,date
Rob,Avanti,12.83,4.0,-21.9,-1.0,"Feb 04, 2016"

javascript regular expression: how do I find date without year or date with year<2010

I need to find date without year, or date with year<2010.
basically,
Feb 15
Feb 20
Feb 20, 2009
Feb 20, 1995
should be accepted
Feb 20, 2010
Feb 20, 2011
should be rejected
How do I do it?
Thanks,
Cheng
Try this:
(Jan|Feb|Mar...Dec)\s\d{1,2},\s([1][0-9][0-9][0-9]|200[0-9])
Note: Expand the month list with proepr names. I was too lazy to spell it all out.