Java 获取各时区时间,获取当前时间到格林威治时间1970年01月01日00时00分00秒的秒数
- 格林威治时间即UTC/GMT时间,1970年01月01日00时00分00秒(即UTC+8的北京时间1970年01月01日08时00分00秒)
计算代码如下:
/**
* 获取指定时间到格林威治时间的秒数
* UTC:格林威治时间1970年01月01日00时00分00秒(UTC+8北京时间1970年01月01日08时00分00秒)
* @param time
* @return
*/
public static long diffSeconds(String time){
Calendar calendar = Calendar.getInstance(); calendar.clear();
Date datetime = DatetimeUtil.toDateByDate14(time);
calendar.setTime(datetime); TimeZone timeZone = TimeZone.getTimeZone("GMT+08:00");
calendar.setTimeZone(timeZone);
return calendar.getTimeInMillis()/1000;
} public static void main(String[] args) throws Exception { String datetime = DatetimeUtil.getDatetime();
System.out.println("=================方法一:calendar============================");
System.out.println(diffSeconds(datetime));
System.out.println("=================方法二:计算时间差============================");
System.out.println(DatetimeUtil.diffSeconds("19700101080000", datetime, DatetimeUtil.PATTERN_YYYYMMDDHHMMSS));
System.out.println("=================方法三:使用system============================");
System.out.println(System.currentTimeMillis()/1000);
}
2. 用Java取指定时区的时间 北京时间,纽约时间,班加罗尔时间
/**
* 取北京时间
* @return
*/
public static String getBeijingTime(){
return getFormatedDateString(8);
} /**
* 取班加罗尔时间
* @return
*/
public static String getBangaloreTime(){
return getFormatedDateString(5.5f);
} /**
* 取纽约时间
* @return
*/
public static String getNewyorkTime(){
return getFormatedDateString(-5);
} /**
* 此函数非原创,从网上搜索而来,timeZoneOffset原为int类型,为班加罗尔调整成float类型
* timeZoneOffset表示时区,如中国一般使用东八区,因此timeZoneOffset就是8
* @param timeZoneOffset
* @return
*/
public static String getFormatedDateString(float timeZoneOffset){
if (timeZoneOffset > 13 || timeZoneOffset < -12) {
timeZoneOffset = 0;
} int newTime=(int)(timeZoneOffset * 60 * 60 * 1000);
TimeZone timeZone;
String[] ids = TimeZone.getAvailableIDs(newTime);
if (ids.length == 0) {
timeZone = TimeZone.getDefault();
} else {
timeZone = new SimpleTimeZone(newTime, ids[0]);
} SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
sdf.setTimeZone(timeZone);
return sdf.format(new Date());
}
Java 获取各时区时间,获取当前时间到格林威治时间1970年01月01日00时00分00秒的秒数的更多相关文章
- java8中计算两个日期时间LocalDateTime的时间差,格式化成xx年yy月zz日aa时bb分cc秒
原则上应该适用Period来计算,因为他是专门为这种需求设计的.当时他只能计算到两个时间差的,年月日 传入参数Period.between(LocalDate,LocalDate) 这里是计算两个Lo ...
- 格林治时间,也就是返回从 UTC 1970 年 1 月 1 日午夜开始经过的毫秒数。
格林治时间,也就是返回从 UTC 1970 年 1 月 1 日午夜开始经过的毫秒数. (* Delphi获取13位格林治时间实现方法, 与java中的java.lang.System.currentT ...
- 【基础篇】DatePickerDialog日期控件的基本使用(二) ——分别获取年、月、日、时、分
项目步骤: 1.在Main.xml布局文件中定义对应的组件,Main.xml内容如下: <?xml version="1.0" encoding="utf-8&qu ...
- SQL获取当前日期的年、月、日、时、分、秒数据
SQL Server中获取当前日期的年.月.日.时.分.秒数据: SELECT GETDATE() as '当前日期',DateName(year,GetDate()) as '年',DateName ...
- SQL Server中如何获取当前年,月,日,时,分,秒
分类: SQL Server select GETDATE() as '当前日期',DateName(year,GetDate()) as '年',DateName(month,GetDate()) ...
- java获取两个时间的相隔时间,包括年、月、日、时、分、秒
public static final int YEAR_RETURN = 0; public static final int MONTH_RETURN = 1 ...
- iOS-获取当前时间的年、月、日、时、分、秒
//获取当前时间02 NSDate *now = [NSDate date];03 NSLog(@”now date is: %@”, now);0405 NSCalendar *c ...
- SQL SERVER数据库,按年、月、日、时、分、秒计算两个时间字段之间的间隔时间样例
使用DATEDIFF(取值,时间字段1,时间字段2) 举例: SELECT DATEDIFF(YEAR,DRYSJ,DCYSJ),* FROM YXHIS2019..TBZYBR2019 --SQL ...
- C# DateTimePicker控件获取他的年,月,日,时,分,秒
CustomFormat属性设置为: yyyy-MM-dd HH:mm:ss 记住还要修改一个属性值,DateFormat属性 可选项改为Custom,默认是Long
- java为啥计算时间从1970年1月1日开始
http://www.myexception.cn/program/1494616.html ————————————————————————————————————————————————————— ...
随机推荐
- 20150612_Andriod contextual action mode 菜单
参考地址:http://www.xuebuyuan.com/1114028.html http://www.cnblogs.com/mengdd/p/3564782.html ...
- sscanf 函数 分类: POJ 2015-08-04 09:19 4人阅读 评论(0) 收藏
sscanf 其实很强大 分类: 纯C技术 技术笔记 2010-03-05 16:00 12133人阅读 评论(4) 收藏 举报 正则表达式stringbuffercurlgoogle 最近在做日志分 ...
- getopts
http://blog.sina.com.cn/s/blog_81c2cf020100v0wh.html http://www.cnblogs.com/xiangzi888/archive/2012/ ...
- UVa 10047,独轮车
题目链接:https://uva.onlinejudge.org/external/100/10047.pdf 题目链接:http://vjudge.net/contest/132239#proble ...
- PHP处理数据--excel与scv与json
今天要处理两个excel.两个循环嵌套验证重复性.所以写了几个函数来处理20亿次的数据量. 一.把excel读出来,保存为json.利用phpexcel插件: <?php header(&quo ...
- JQuery多媒体插件jQuery Media Plugin使用详解
malsup jquery media plugin 该插件可以播放多种类型的多媒体文件包括:Flash, Quicktime, Windows Media Player, Real Player, ...
- 04-23 Android 课堂笔记
1.调用 //暂停 @Override protected void onPause() { super.onPause(); Log.e("TAG", "onPause ...
- linux下的基本网络配置
第一种:使用命令修改(直接即时生效,重启失效)#ifconfig eth0 192.168.0.1 netmask 255.255.255.0 up说明:eth0是第一个网卡,其他依次为eth1,et ...
- Linux常用命令大杂烩(持续更新)
1.vimn,$s/findstr/targetstr/g #替换n到文档末尾的所有字符串:% s/^.\{4\}//g #将当前缓冲区的所有行的前4个字符删除 2.每周日早上3:30删除日志30 3 ...
- Common Macros for Build Commands and Properties
https://msdn.microsoft.com/en-us/library/c02as0cs.aspx $(ProjectDir) The directory of the proje ...