본문 바로가기

카테고리 없음

FreeMarker Date 이용하기


스트링을 이용하여 date 처리하기

이 빌트인은 날짜타입을 문자타입으로 변경한다. 기본 타입은 date_format, time_format 그리고 datetime_format 것을 바탕으로 기술된 것이다.

서전에 정의된 포맷을 이용해도 되고, 원하는 ㅎ여식대로 포매팅해서 처리할 수 있다.

사전 정의된 포맷은 short, medium, long, full이 있으며, 다음 예에서 확인해보자. 이것은 로케일이 US English를 기반으로 한다.

${openingTime?string.short}
${openingTime?string.medium}
${openingTime?string.long}
${openingTime?string.full}

${nextDiscountDay?string.short}
${nextDiscountDay?string.medium}
${nextDiscountDay?string.long}
${nextDiscountDay?string.full}

${lastUpdated?string.short}
${lastUpdated?string.medium}
${lastUpdated?string.long}
${lastUpdated?string.full}  

will prints something like this:

12:45 PM
12:45:09 PM
12:45:09 PM CEST
12:45:09 PM CEST

4/20/07
Apr 20, 2007
April 20, 2007
Friday, April 20, 2007

4/20/07 12:45 PM
Apr 20, 2007 12:45:09 PM
April 20, 2007 12:45:09 PM CEST
Friday, April 20, 2007 12:45:09 PM CEST  


short, medium, long과 full은 현재 언어 로케일에 따라간다. 더군다나 이것은 FreeMarker에 의해서 지정된 것이 아니다.

날짜는 date파트와 time파트로 나눠져 있다. 그리고 서로 독립적으로 date와 time의 길이를 설정해 줄 수 있다.

${lastUpdated?string.short_long} <#-- short date, long time -->
${lastUpdated?string.medium_short} <#-- medium date, short time -->  

will output:

4/8/03 9:24:44 PM PDT
Apr 8, 2003 9:24 PM  

노트 ?string.short는 ?string.short_short와 동일하고 ?string.medium은 string.medium_medium과 동일하다.

#주의#

불행하게도 자바 플랫폼의 한계때문에, date 변수가 데이터모델이 있을경우 FreeMarker은 오직 날짜 타입(year, month, day) 부분만 저장하거나 time 파트 (hour, minute, second, millisecond) 만을 저장할 수 없다. 프리마커는 ${lastUpdated?string.short}${lastUpdated} 로 단순하게 기술했다면 날짜를 어떻게 표현해야할지 알지 못하고, 에러를 낼 것이다. 이를 막기 위해서 프리마커의 ?date, ?time and ?datetime built-ins. 를 이용해야한다. 예를 들어 ${lastUpdated?datetime?string.short}. 와 같이 처리해야한다. 만약 특정 데이터 모델이 이러한 문제를 가지고 있다면 항상 ?date, ?time ,?datetime 을 사용하도록 요청할 것이다.

사전에 정의된 포맷을 이용하는 대신에 특정 포맷을 지정하여 표현할 수 있다. 이것은 자바 패턴을 따라가며 ?string(pattern_string) 형식으로 사용한다.

${lastUpdated?string("yyyy-MM-dd HH:mm:ss zzzz")}
${lastUpdated?string("EEE, MMM d, ''yy")}
${lastUpdated?string("EEEE, MMMM dd, yyyy, hh:mm:ss a '('zzz')'")}  

will output:

2003-04-08 21:24:44 Pacific Daylight Time
Tue, Apr 8, '03
Tuesday, April 08, 2003, 09:24:44 PM (PDT)  

#노트#

사전 정의된 형식과는 달리, ?date, ?time, ?datetime를 이용하지 않는다. 이것은 프리마커에 날짜 타입의 특정 부분을 표현하게끔 패턴을 지정해주면 된다.  ${openingTime?string("yyyy-MM-dd hh:mm:ss a")}openingTime 이것이 오직 time만 가지고 있다면  1970-01-01 09:24:44 PM. 게 표현된다.

패턴 스트링은 또한 "short", "medium", ..., "short_medium", ... 기타 표현을 사용할 수 있다. 이것은 사전 정의된 포맷을 이용할때와 동일한 결과를 나타내며 someDate?string("short") , someDate?string.short 로 이용할 수 있다.

참조: the interpolation of dates

date, time, datetime

이 빌트인은 사용되는 날짜의 특정 부분만을 이용할 수 있도록 해준다.

  • date: 오직 년,월,일 부분만 이용할수 있다.

  • time: 오직 시,분,초,밀리초만을 이용할 수 있다.

  • datetime: 날짜와 시간 모든 부분을 이용할 수 있다.

최적의 케이스는 이러한 빌트인이 필요 없는 것일 것이다. 불항해게도 자바의 기술적인 한계로 인해서 제약이 있다는 것이다. 프리마커는 날짜의 특정 부분만을 검색하지 못한다. (i.e. only the year+month+day, or only hour+minute+second+millisecond, or both) 와 같은 부분은 가끔식 검색되지 못한다. 만약 이러한 문제가 발생하는 경우에는 에러가 나고 더이상 진행하지 못하게 된다. 다음 예제는 openingTime이 그러한 문제를 가지고 잇다고 가정한다.

<#assign x = openingTime> <#-- no problem can occur here -->
${openingTime?time} <#-- without ?time it would fail -->
<#-- For the sake of better understanding, consider this: -->
<#assign openingTime = openingTime?time>
${openingTime} <#-- this will work now -->  

There is another usage of these built-ins: to truncate dates. For example:

Last updated: ${lastUpdated} <#-- assume that lastUpdated is a date-time value -->
Last updated date: ${lastUpdated?date}
Last updated time: ${lastUpdated?time}  

will output something like:

Last updated: 04/25/2003 08:00:54 PM
Last updated date: 04/25/2003
Last updated time: 08:00:54 PM  

If the left side of the ? is string, then these built-ins convert strings to date variable.

출처 : http://freemarker.sourceforge.net/docs/ref_builtins_date.html