Rumah  >  Artikel  >  pembangunan bahagian belakang  >  velocity第9个应用例子 ---格式化日期

velocity第9个应用例子 ---格式化日期

黄舟
黄舟asal
2017-01-17 11:02:471463semak imbas

扩展功能,可以写个工具类,然后把工具类放到context中,在模板中可以直接调用工具类的方法

默认情况下,日期输出的Tue
Jul 14 16:42:30 CST 2015

我们需要写个工具类,对日期进行格式化,

把原日期和日期格式化工具类都放入到context中

在模板中,调用日期格式化工具类的方法,对日期进行格式化

$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)

以上就是velocity第9个应用例子 ---格式化日期的内容,更多相关内容请关注PHP中文网(www.php.cn)!


Kenyataan:
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn