DateTime获取当地时间
2015-10-19 20:37
260 查看
Joda是一个便于使用的时间处理库
官网地址
http://www.joda.org/joda-time/
基本的使用网络上已经很多了,说一下转换到当地时区的方法。
从时间戳转为当地时间,以中国时间为例
输出:
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
可以看到是一样的。
用处:可以用来分析日志的时间戳。
官网地址
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
可以看到是一样的。
用处:可以用来分析日志的时间戳。
相关文章推荐
- 程序员既要写好代码,又要写好文档
- C++primer学习:拷贝控制(4)
- Mybatis系列目录篇
- hadoop-源码原理剖析--------(一)手把手教你如何进行hadoop开发的环境搭建,拒绝深坑!~
- UI23_多线程
- nginx与tomcat集群
- 北京Uber优步司机奖励政策(10月19日~10月25日)
- 在Windows 7 (SP1)上安装Visual Studio 2015
- 黑马程序员--Java学习日记之集合(collection类和map类代码)
- 初学c课程笔记整理1->>概念初解
- 从头学Qt Quick(2)-- QML语法从一个简单的例子说起
- apk文件签名绕过
- Android功能之第三方SwipeMenuListView,Item项的侧滑
- Python下ORM的一个设计举例
- 高效的CSS代码(1)
- 工作第一天
- 去哪儿MySQL开发规范
- Chapter 15 Dynamic Programming
- 数据结构例程——二叉树遍历的递归算法
- javascript之DOM操作