Requirement: Get a variable lastclock from a third party, with a forward value of 1305575275540 and a timestamp. Use freemarker to convert the variable to a common date format on the page.
Import Java. io. stringwriter; import Java. util. hashmap; import Java. util. map; import freemarker. cache. stringtemplateloader; import freemarker. template. configuration; import freemarker. template. template; public final class freemarkutils {Private Static configuration CFG = new configuration (); Private Static string key = "key "; /*** return the rendered value of freemark Based on the template * @ Param name * @ Param map * @ return freemark rendered value * @ thro WS exception * @ author chenqing */public static string gettemplatevalue (string name, Map <string, Object> map) throws exception {stringtemplateloader = new stringtemplateloader (); stringtemplateloader. puttemplate (Key, name); cfg. settemplateloader (stringtemplateloader); Template T = cfg. gettemplate (key); stringwriter out = new stringwriter (); T. process (MAP, out); Return out. tostring () ;}/*** Directly return the rendered value of freemark * @ Param name * @ return freemark the rendered value * @ throws exception */public static string gettemplatevalue (string name) throws exception {return gettemplatevalue (name, new hashmap <string, Object> ();}/*** @ Param ARGs */public static void main (string [] ARGs) {try {Map <string, Object> root = new hashmap <string, Object> (); root. put ("lastclock", "1305575275540"); string out = freemarku Tils. gettemplatevalue ("$ {lastclock? Number? Number_to_datetime} ", root); system. Out. println (out);} catch (exception e) {e. printstacktrace ();}}}
The preceding method can be extended and used for packaging string output, not limited to web pages.
Freemarker timestamp to date