Home > Backend Development > PHP Tutorial > Velocity's 9th application example --- format date

Velocity's 9th application example --- format date

黄舟
Release: 2023-03-04 21:36:01
Original
1552 people have browsed it

To extend the function, you can write a tool class, and then put the tool class in the context. You can directly call the tool class method in the template

By default, the date output is Tue
Jul 14 16:42:30 CST 2015

We need to write a tool class to format the date,

Put the original date and date formatting tool class 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 object

VelocityContext context = newVelocityContext();

//3 Add you data object to this context

context.put("date", new Date());

//扩展功能,提供一个日期格式工具类,在模板中调用其方法即可。

context.put("dateformat", newDateUtils());

//4 Choose a template

Template template =Velocity.getTemplate("formatedate.vm");

//5 Merge the template and you data toproduce the output

StringWriter sw = new StringWriter();

template.merge(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)
Copy after login

The above is the ninth application example of velocity---the content of formatting the date. For more related information, please Follow the PHP Chinese website (m.sbmmt.com)!


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template