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 ————————————————————————————————————————————————————— ...
随机推荐
- csharp通过dll调用opencv函数,图片作为参数
[blog 项目实战派]csharp通过dll调用opencv函数,图片作为参数 一直想做着方面的研究,但是因为这个方面的知识过于小众,也是由于自己找资料的能力比较弱,知道今天才找 ...
- C#字符串题目
老师给小学生门布置了一些作业,让它们按照一个模版写一些字符串交上来,同学们把作业交上来了,问题来了,这么多的作业老师批改不过来,现在请你帮老师写一个程序,帮助老师确定各个字符串是否合格.首先老师有一个 ...
- springmvc配置文件-1
项目1: web.xml <?xml version="1.0" encoding="UTF-8" standalone="no"?& ...
- COM技术の组件
什么是COM COM,Component Object Mode即组件对象模型.之所以称之为“模型”,是表明COM是一种编程规范(非具体代码),通过这种规范我们能够编写出语言无关的,可扩展的,内部变化 ...
- linux 切换多个jdk脚本
1,编写脚本 jdkswitch.sh #!/bin/sh # usage: . this_file [argvs] openjdk7_home=/usr/lib/jvm/java--openjdk- ...
- 压缩和解压缩gz包
gz是Linux和OSX中常见的压缩文件格式,下面是用java压缩和解压缩gz包的例子 public class GZIPcompress { public static void FileCompr ...
- 使用js实现移动设备访问跳转到指定目录
最近最项目的时候总会同时做pc站点跟手机站点,当手机访问的时候默认是看到pc站点的,需要在url上加上/mobile才能正常访问,这段代码是我同事分享给我的,还是蛮实用的. CODE function ...
- SqlSever基础 一个条件group by 一列有两个内容,分组并查看每个内容有多少行,并用as起名
镇场诗:---大梦谁觉,水月中建博客.百千磨难,才知世事无常.---今持佛语,技术无量愿学.愿尽所学,铸一良心博客.------------------------------------------ ...
- C#多线程之二:ManualResetEvent和AutoResetEvent
初次体验 ManualResetEvent和AutoResetEvent主要负责多线程编程中的线程同步:以下一段是引述网上和MSDN的解析: 在.Net多线程编程中,AutoResetEvent和Ma ...
- JAVA——getter setter
package org.hanqi.pn0120; public class User { private int userid; private String username; private S ...