Velocity 9th Application Example---formatted date

Source: Internet
Author: User
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)!

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.