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 ————————————————————————————————————————————————————— ...
随机推荐
- hdu1074 Doing Homework
这题比较有意思,暴力搜索必然tle,可以用状态压缩dp解决. 我们先不考虑完成所有作业的扣分,而考虑其一个子集的情况. 假设我们得到了完成某子集S对应的作业最少扣分,我们试着向该子集中增加一个元素a, ...
- Android WebView如何加载assets下的html文件
项目需求:将html文件以及所用到的图片都放在 assets/html/ 目录下.然后在页面上通过WebView来显示 直接付上代码: private void readHtmlFormAssets( ...
- U3D UGUI学习1 - 层级环境
就像主动碰撞检测需要刚体,刚体需要Mesh Filter.Unity的一些组件都需要基础配置. UGUI的基础配置和NGUI差不多,把NGUI的那些中间件全部削减干净,甚至连快捷键也没有. 1.基础层 ...
- 【转】【调试技巧】Linux环境下段错误的产生原因及调试方法小结
本文转自:http://www.cnblogs.com/panfeng412/archive/2011/11/06/segmentation-fault-in-linux.html 1. 段错误是什么 ...
- zoj Abs Problem
Abs Problem Time Limit: 2 Seconds Memory Limit: 65536 KB Special Judge Alice and Bob is pl ...
- AVL学习笔记
AVL,平衡二叉查找树.删除,插入,查找的复杂度都是O(logn).它是一棵二叉树.对于每个节点来说,它的左孩子的键值都小于它,右孩子的键值都大于它.对于任意一个节点,它的左右孩子的高度差不大于1.树 ...
- JAVA 语言基础——运算符
1.赋值运算符 赋值运算符"=",是一个能对两个操作数进行处理的二元运算符. 比如: int a = 12; //声明int型变量a int b= 34; //声明int型变量b ...
- nodejs链接mysql数据库,执行简单的增删改查操作
var mysql = require('mysql'); var conn = mysql.createConnection({ host: 'localhost', user: 'root', p ...
- CodeForces 148B Escape
Escape Time Limit:2000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64u Submit Stat ...
- C# POST与Get数据
引用DLL 普通Get数据和Post数据 public static string Get(string URL) { String ReCode = string.Empty; try { Http ...