Extension function, you can write a tool class, and then put the tool class into the context, in the template can directly invoke the method of the tool class
By default, the Tue of the date output
Jul 16:42:30 CST 2015
We need to write a tool class to format the date,
Put the original date and date formatting tool classes into the context
In the template, call the method of the date formatting tool class to format the date
$dateformat. Format ("Yyyy-mm-dd", $date)//2 Create a context Objectvelocitycontext context = Newvelocitycontext ();//3 ADD you data object to this contextcontext.put ("date", new Date ()), or//extension, which provides a date format tool class that calls its methods in the template. Context.put ("DateFormat", Newdateutils ())//4 Choose a templatetemplate template =velocity.gettemplate (" FORMATEDATE.VM ");//5 Merge the template and you data toproduce the outputstringwriter SW = new StringWriter (); Template.mer GE (context, SW); Sw.flush (); System.out.println (Sw.tostring ()); formatedate.vm${date}=== $date = = = $dateformat. Format ("Yyyy-mm-dd", $date)-==$ Dateformat.format ("Yyyy-mm-ddhh:mm:ss", $date) = = $dateformat. Format ("YyyyMMdd", $date)
The above is the velocity of the 9th application example---Format the content of the date, more relevant content please pay attention to topic.alibabacloud.com (www.php.cn)!