Joda是一个便于使用的时间处理库
官网地址
http://www.joda.org/joda-time/
基本的使用网络上已经很多了,说一下转换到当地时区的方法。
从时间戳转为当地时间,以中国时间为例
//指定时间,从1970-01-01 00:00:00算起的时间毫秒数
long time =1444983974078L ;
DateTime dateTimeGMT = new DateTime(time);
//设定时区为东八区:GMT-8表示比格林威治时间快8个时区的时区减去8才是格林威治时间,也就是东八区
DateTimeZone dateTimeZoneGMT = DateTimeZone.forID("Etc/GMT-8");
dateTimeGMT = dateTimeGMT.withZone(dateTimeZoneGMT);
//输出东八区时间
System.out.println("UTC+8标准时间:"+dateTimeGMT.toString());
//或者和JDK保持一致取上海"Asia/Shanghai"
DateTime dateTimeZH_CN = new DateTime(time);
DateTimeZone dateTimeZoneZH_CN = DateTimeZone.forID("Asia/Shanghai");
dateTimeZH_CN = dateTimeZH_CN.withZone(dateTimeZoneZH_CN);
//输出中国上海时间
System.out.println("中国上海标准时间:"+dateTimeZH_CN.toString());
DateTime nowDateTime = new DateTime(new Date().getTime());
//设定时区为东八区:GMT-8表示比格林威治时间快8个时区的时区减去8才是格林威治时间,也就是东八区
nowDateTime= nowDateTime.withZone(DateTimeZone.forID("Etc/GMT-8"));
System.out.println("UTC+8标准时间:"+nowDateTime.toString());
输出:
UTC+8标准时间:2015-10-16T16:26:14.078+08:00
中国上海标准时间:2015-10-16T16:26:14.078+08:00
UTC+8标准时间:2015-10-19T21:54:12.306+08:00
可以看到是一样的。
用处:可以用来分析日志的时间戳。