Freemarker时间戳转日期

需求:从第三方获取一个变量lastclock,里面值是1305575275540,一串时间戳,在页面用Freemarker将之转换成常见日期格式。

import java.io.StringWriter;
import java.util.HashMap;
import java.util.Map;

import freemarker.cache.StringTemplateLoader;
import freemarker.template.Configuration;
import freemarker.template.Template;

public final class FreemarkUtils {
	private static Configuration cfg = new Configuration();
	private static String key="key";

	/**
	 * 根据模板返回Freemark渲染后的值
	 * @param name
	 * @param map
	 * @return Freemark渲染后的值
	 * @throws Exception
	 * @author chenqing
	 */
	public static String getTemplateValue(String name, Map<String, Object> map) throws Exception {
		StringTemplateLoader stringTemplateLoader = new StringTemplateLoader();
		stringTemplateLoader.putTemplate(key, name);
		cfg.setTemplateLoader(stringTemplateLoader);

		Template t = cfg.getTemplate(key);
		StringWriter out = new StringWriter();

		t.process(map, out);
		return out.toString();
	}

	/**
	 * 直接返回Freemark渲染后的值
	 * @param name
	 * @return Freemark渲染后的值
	 * @throws Exception
	 */
	public static String getTemplateValue(String name) throws Exception {
		return getTemplateValue(name, new HashMap<String, Object>());
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		try {
			Map<String, Object> root = new HashMap<String, Object>();
			root.put("lastclock", "1305575275540");
			String out = FreemarkUtils.getTemplateValue("${lastclock?number?number_to_datetime}", root);
			System.out.println(out);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}

上面的方法可以扩展,用于字符串输出的包装,不局限于web页。

时间: 2024-07-30 09:16:11

Freemarker时间戳转日期的相关文章

FreeMarker中的日期时间处理

1. FreeMarker中的日期时间格式设置 FreeMarker中可以分别对date.time.datetime三种类型的日期时间设置格式,例如: config.setDateTimeFormat("yyyy-MM-dd HH:mm:ss"); config.setDateFormat("yyyy-MM-dd"); config.setTimeFormat("HH:mm:ss"); 当我们对一个页面变量使用 ?date ?time ?date

php中时间戳和日期格式的转换

一,PHP时间戳函数获取指定日期的unix时间戳 strtotime(”2009-1-22″) 示例如下: echo strtotime(”2009-1-22″) 结果:1232553600 说明:返回2009年1月22日0点0分0秒时间戳 二,PHP时间戳函数获取英文文本日期时间 示例如下: 便于比较,使用date将当时间戳与指定时间戳转换成系统时间 (1)打印明天此时的时间戳strtotime(”+1 day”) 当前时间:echo date(”Y-m-d H:i:s”,time()) 结果

mysql UNIX时间戳与日期的相互转换

UNIX时间戳转换为日期用函数: FROM_UNIXTIME() select FROM_UNIXTIME(1156219870); 日期转换为UNIX时间戳用函数: UNIX_TIMESTAMP() Select UNIX_TIMESTAMP(’2006-11-04 12:23:00′); 例:mysql查询当天的记录数: $sql=”select * from message Where DATE_FORMAT(FROM_UNIXTIME(chattime),’%Y-%m-%d’) = DA

js 时间戳转为日期格式

js 时间戳转为日期格式 什么是Unix时间戳(Unix timestamp): Unix时间戳(Unix timestamp),或称Unix时间(Unix time).POSIX时间(POSIX time),是一种时间表示方式,定义为从格林威治时间1970年01月01日00时00分00秒起至现在的总秒数.Unix时间戳不仅被使用在Unix系统.类Unix系统中,也在许多其他操作系统中被广泛采用. 目前相当一部分操作系统使用32位二进制数字表示时间.此类系统的Unix时间戳最多可以使用到格林威治

java 日期转时间戳,时间戳转为日期

package date; import java.text.ParseException;import java.text.SimpleDateFormat;import java.util.Date; public class test { public static void main(String[] args) { Date d = new Date(); String beginDate = "1435845268096"; SimpleDateFormat sdf = n

日期转时间戳,时间戳转日期

日期转时间戳 public Int64 ToUnixTimestamp(DateTime date)        {            DateTime unixEpoch = new DateTime(1970, 1, 1, 0, 0, 0);            TimeSpan unixTimeSpan = date - unixEpoch;            return (Int64)unixTimeSpan.TotalSeconds-28800;        } 时间戳

jquery 时间戳和日期时间转化

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 (function($) {     $.extend({         myTime: {             /**              * 当前时间戳          

js时间戳转为日期格式

这个在php+mssql(日期类型为datetime)+ajax的时候才能用到,js需要把时间戳转为为普通格式,一般的情况下可能用不到 [php] view plaincopy <script> function getLocalTime(nS) { return new Date(parseInt(nS) * 1000).toLocaleString().replace(/:\d{1,2}$/,' '); } alert(getLocalTime(1293072805)); </scr

php日期转时间戳,指定日期转换成时间戳

写过PHP+MySQL的程序员都知道有时间差,UNIX时间戳和格式化日期是我们常打交道的两个时间表示形式,Unix时间戳存储.处理方便,但 是不直观,格式化日期直观,但是处理起来不如Unix时间戳那么自如,所以有的时候需要互相转换,下面给出互相转换的几种转换方式. 一.在MySQL中完成 这种方式在MySQL查询语句中转换,优点是不占用PHP解析器的解析时间,速度快,缺点是只能用在数据库查询中,有局限性. 1. UNIX时间戳转换为日期用函数: FROM_UNIXTIME() 一般形式:sele