tl  tr
  Home | Tutorials | Articles | Videos | Products | Tools | Search
Interviews | Open Source | Tag Cloud | Follow Us | Bookmark | Contact   
 Template Engines > Freemarker > Date Time Format Settings

Date Time Format Settings 

FreeMarker is a java based template engine for complex template processing. This requires the library freemarker-2.3.16.jar to be in classpath. The following example shows various date and time format settings in FreeMarker.

File Name  :  
/FREEMARKER001/config/datetime.ftl 

File Name  :  
com/bethecoder/tutorials/freemarker/tests/DateTimeSettingsTest.java 
Author  :  Sudhakar KV
Email  :  [email protected]
   
package com.bethecoder.tutorials.freemarker.tests;

import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;

public class DateTimeSettingsTest {

  /**
   @param args
   @throws IOException 
   @throws TemplateException 
   */
  public static void main(String[] argsthrows IOException, TemplateException  {

    //Get template from classpath
    Configuration cfg = new Configuration();
    cfg.setClassForTemplateLoading(DateTimeSettingsTest.class, "/");
    Template template = cfg.getTemplate("datetime.ftl");
    
    //Prepare data model
    Map<String, Object> dataModel = new HashMap<String, Object>();
    dataModel.put("bday"new Date());
    
    //Merge template and data
    OutputStreamWriter output = new OutputStreamWriter(System.out);
    template.process(dataModel, output);
  }
}
   

It gives the following output,
Date : Nov 25, 2011
Time : 10:04:58 PM
Time : Nov 25, 2011 10:04:58 PM


Date : 11/25/2011
Time : 10:04 PM
Time : November 25, 2011 10:04:58 PM



 
  


  
bl  br