2

how can i create an date object in ftl. the below code isn't helping me

<#assign now2 = new java.util.Date()>

can anyone provide me a link on tutorial for working with dates in ftl the programmer guide of ftl is not clear

Mateen
  • 1,631
  • 1
  • 23
  • 27

3 Answers3

1

A new date with what value? If you need the current time, you can use the .now variable. Otherwise you can use something like ${'2014-01-05T23:30:00'?datetime.iso}.

ddekany
  • 29,656
  • 4
  • 57
  • 64
  • thanks i have used .now variable it not working with me as i working with fremarker-2.3.11.jar and this is error displayed when i used your answer Can't parse iso to a date format. The problematic instruction: ---------- ==> ${'2014-01-05T23:30:00'?datetime.iso} – Mateen Jan 09 '15 at 04:43
  • Don't work with 2.3.11... it's 7 years old. And yes, it had no `iso` format. Use the latest. – ddekany Jan 09 '15 at 18:01
1

This isn't the answer you're looking for, but it may be useful anyway.

You probably ought to ask yourself why you want to create a date within the FTL template. FTL is powerful, but its core strength is controlling the presentation of data from another source. In the case where I've used FTL, the source of the data is generally a model object produced by the controller object, and the relationship of the FTL view to the others is managed through Spring MVC. In a design like that, getting the current date into the FTL view is as simple as adding a model attribute:

   model.addAttribute("date", new java.util.Date());

FTL then has facilities for rendering the date in configurable fashion. This post talks about how to configure date time rendering within FTL, as well as how to integrate JodaTime into Freemarker so that it can be rendered as well.

mschaef
  • 1,620
  • 10
  • 12
  • 1
    thanks for the answer actually i m learning ftl so i had this confusion while working with dates and, what is model is it a predefined object like request,session,..etc in jsp ?? – Mateen Jan 09 '15 at 04:48
  • More or less, yes. The model is a HashMap-like structure that the controller layer populates with data to be displayed by the FTL template. All of the key/value pairs in the model are globally available within the FTL template. At least in Spring MVC, the general control flow is that the HTTP endpoint accepts a request, the spring dispatcher servlet hands it off to a controller object within the context. The controller then gathers the data needed to handle the request, populates a model with that data, and then hands off control to a FTL view by name. – mschaef Jan 09 '15 at 16:11
  • thanks for the explanation, but i m neither using spring nor any other context i am just learning things more basically by keeping everything in a single page. if i use some advance frameworks like spring + ftl your answer would make much sense to me. – Mateen Jan 09 '15 at 16:49
  • 1
    The same generic pattern still applies. FreeMarker is for pure MVC. No request, session etc. Just the "business data" in the data-model (as FM calls it). The data-model is usually JavaBean or a `Map`. – ddekany Jan 09 '15 at 18:03
0

Following is the example on how can we create date object

example1 :

<#assign objectConstructor2 = "freemarker.template.utility.ObjectConstructor"?new()>
<#assign nowObj2 = objectConstructor("java.util.Date")>
<#assign mmddyy2 = objectConstructor("java.text.SimpleDateFormat","MM/dd/yyyy")>
<#assign now2 = mmddyy2.format(nowObj2)>

example 2: Freemarker print date in template

Community
  • 1
  • 1
Mateen
  • 1,631
  • 1
  • 23
  • 27
  • Don't do this. `ObjectConstructor` is an unfortunate legacy, but it will be banned/removed as quickly as backward compatibility concerns allow it. – ddekany Jan 08 '15 at 18:30