日期:2014-05-17  浏览次数:20755 次

velocity如何对日期进行格式化
例如,在数据库里日期格式是 2008-01-01 20:10:50
在velocity里要如何处理才可以只显示2008-01-01 

显示方式可以在velocity模板里控制的那种...不受java影响.....

------解决方案--------------------
Java code

import java.io.StringWriter;
import java.io.Writer;
import java.util.Calendar;
import java.util.TimeZone;

import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.Velocity;
import org.apache.velocity.tools.generic.DateTool;

public class DateToolExample {

  public static void main(String[] args) throws Exception {
    Velocity.init();

    Template t = Velocity.getTemplate("./src/dateTool.vm");

    VelocityContext ctx = new VelocityContext();
    ctx.put("date", new DateTool());

    Calendar aDate = Calendar.getInstance(TimeZone.getTimeZone("PST"));
    aDate.set(200, 11, 25);

    ctx.put("aDate", aDate);

    Writer writer = new StringWriter();
    t.merge(ctx, writer);

    System.out.println(writer);
  }
}