Please Enable JavaScript!
Gon[ Enable JavaScript ]

반응형
velocity 페이지에서 java 제공하는 date, 시간관련 객체를 사용하고싶을때
Calender class object 사용하여 Date class object 를 생성한후 velocity 넘겨준다.

Date date = cal.getTime(): Here we created Date class object by using Calender class object.
$date.getHours() : getHours() is used here to print hours
$date.getMinutes() : getMinutes() is used here to print minutes
$date.getSeconds() : getSeconds() is used here to print seconds

$date.getDate() : getDate() is used here to print  date
$date.getMonth() : getMonth() is used here to print month
$date.getYear() : getYear() is used here to print year

import java.io.*;
import java.util.*;
import org.apache.velocity.*;
import org.apache.velocity.app.*;

public class DateToolClass {
    public static void main(String[] args) throws Exception {
        Velocity.init();
        Template template = Velocity.getTemplate("./src/dateTool.vm");
        VelocityContext context = new VelocityContext();
        Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("PST"));
        Date date = cal.getTime();
        context.put("date", date);
        
        Writer writer = new StringWriter();
        template.merge(context, writer);
        System.out.println(writer);
    }
}

 

======= input : dateTool.vm =====

$date;

 

Time      $date.getHours():$date.getMinutes():$date.getSeconds()

Date      $date.getDate()/$date.getMonth()/$date.getYear()

 

====== Output ======

Fri Aug 22 17:15:15 IST 2008;

 

Time      17:15:15

Date      22/7/108te, getMonth() is used here to print month, getYear() is used here to print year.



반응형
Posted by 녹두장군1
,