UNIX时间戳的应用-JAVA

概念:
UNIX时间戳:Unix时间戳(英文为Unix epoch, Unix time, POSIX time 或 Unix timestamp)
是从1970年1月1日(UTC/GMT的午夜)开始所经过的秒数,不考虑闰秒。
UNIX时间戳的0按照ISO 8601规范为 :1970-01-01T00:00:00Z.
一个小时表示为UNIX时间戳格式为:3600秒;一天表示为UNIX时间戳为86400秒,闰秒不计算。
在大多数的UNIX系统中UNIX时间戳存储为32位,这样会引发2038年问题或Y2038。
时间 秒
1 分钟 60 秒
1 小时 3600 秒
1 天 86400 秒
1 周 604800 秒
1 月 (30.44 天) 2629743 秒
1 年 (365.24 天) 31556926 秒



思考:
为什么使用UNIX时间戳?
在现在的系统中经常遇到跨数据库的应用开发,在数据库系统中不同的数据库对与时间类型却有不同解释,比如ORACLE的date和MYSQL里面的date就不能直接兼容转换,数据方面还可以使用数据迁移工具进行转换,但是对与应用来说那就是灾难(在这不谈hibernate等可以垮数据库的框架)。
为了实现垮平台在应用系统中记录时间的时候我们就可以使用记录UNIX时间戳的方法做到垮平台性。
现在大多数的语言java、PHP、Perl等都支持直接取UNIX时间戳,将需要记录的时间记录为UNIX时间戳,这样就可以不同的数据库系统中的垮平台性,对与时间的操作只要对时间戳操作就行了。其实这样的处理方法在很多开源的系统中都能看到,但是对与经常搞J2EE的人来说很不习惯,还好我之前有PHP的底子。

处理:

下面简单介绍下相关处理的方法:
================================获取系统UNIX时间戳=======================================
Perl time
PHP time()
Ruby Time.now (or Time.new). To display the epoch: Time.now.to_i
Python import time first, then time.time()
Java long epoch = System.currentTimeMillis()/1000;
Microsoft .NET C# epoch = (DateTime.Now.ToUniversalTime().Ticks - 621355968000000000) / 10000000;
VBScript/ASP DateDiff("s", "01/01/1970 00:00:00", Now())
MySQL SELECT unix_timestamp(now())
PostgreSQL SELECT extract(epoch FROM now());
SQL Server SELECT DATEDIFF(s, '19700101', GETDATE())
JavaScript Math.round(new Date().getTime()/1000.0) getTime() returns time in milliseconds.
Unix/Linux date +%s
Other OS's Command line: perl -e "print time" (If Perl is installed on your system)



================================将时间转换成UNIX时间戳=======================================

Perl Use these Perl Epoch routines
PHP mktime(hour, minute, second, month, day, year)
Ruby Time.local(year, month, day, hour, minute, second, usec ) (or Time.gm for GMT/UTC input). To display add .to_i
Python import time first, then int(time.mktime(time.strptime('2000-01-01 12:34:00', '%Y-%m-%d %H:%M:%S')))
Java long epoch = new java.text.SimpleDateFormat ("dd/MM/yyyy HH:mm:ss").parse("01/01/1970 01:00:00");
VBScript/ASP DateDiff("s", "01/01/1970 00:00:00", time field)
MySQL SELECT unix_timestamp(time) Time format: YYYY-MM-DD HH:MM:SS or YYMMDD or YYYYMMDD
PostgreSQL SELECT extract(epoch FROM date('2000-01-01 12:34'));
With timestamp: SELECT EXTRACT(EPOCH FROM TIMESTAMP WITH TIME ZONE '2001-02-16 20:38:40-08');
With interval: SELECT EXTRACT(EPOCH FROM INTERVAL '5 days 3 hours');
SQL Server SELECT DATEDIFF(s, '19700101', time field)
JavaScript use the JavaScript Date object
Unix/Linux date +%s -d"Jan 1, 1980 00:00:01"



================================将UNIX时间戳转换成时间=======================================

Perl Use these Perl Epoch routines
PHP date(output format, epoch); Output format example: 'r' = RFC 2822 date
Ruby Time.at(epoch)
Python import time first, then time.gmtime(epoch) time is an array of year, month, day, hour, min, sec, day of week, day of year, DST
Java String date = new java.text.SimpleDateFormat("dd/MM/yyyy HH:mm:ss").format(new java.util.Date (epoch*1000));
VBScript/ASP DateAdd("s", epoch, "01/01/1970 00:00:00")
PostgreSQL SELECT TIMESTAMP WITH TIME ZONE 'epoch' + epoch * INTERVAL '1 second';
MySQL from_unixtime(epoch, optional output format) The default output format is YYY-MM-DD HH:MM:SS
SQL Server DATEADD(s, epoch, '19700101')
JavaScript use the JavaScript Date object
Linux date -d @1190000000 (replace 1190000000 with your epoch, needs newer version of date)
Other OS's Command line: perl -e "print scalar(localtime(epoch))" (If Perl is installed) Replace 'localtime' with 'gmtime' for GMT/UTC time.



UNIX时间戳的应用-JAVA
=============================
非特殊说明,引用
xZeus.org thendmx@gmail.com
=============================

概念:

System.currentTimeMillis() :返回当前系统的毫秒数,由于取得的是毫秒数,所以在处理UNIX时间戳的时候需要转换成秒
也就是:
long epoch = System.currentTimeMillis()/1000;

方法:

1、获取当前系统的UNIX时间戳
System.out.println("获取系统毫秒数方法1:"+Long.toString(new Date().getTime()));
System.out.println("获取系统毫秒数方法2:"+Long.toString(System.currentTimeMillis()));
注意:以上代码获取的都是系统毫秒数,在实际的操作中我们一般都是记录毫秒说以求记录的精度,当处理UNIX时间戳的时候需要把数据进行处理。

2、将UNIX时间戳转换成系统可以处理的时间
System.out.println(""+new java.text.SimpleDateFormat("yyyy MM-dd HH:mm:ss").format(new java.util.Date (1215782027390L)));
输出:2008 07-11 21:13:47
注意:此时处理的数据为系统毫秒不是UNIX时间戳

3、讲时间转换成UNIX时间戳
long epoch = new java.text.SimpleDateFormat ("dd/MM/yyyy HH:mm:ss").parse("09/22/2008 16:33:00").getTime();

注意:

请注意!对与不同的时区处理上有差异,首先要清楚自己所在的时区。
String timezone_info = System.getProperty("user.timezone");
System.out.println("当前的时区:"+timezone_info);
System.out.println("时区信息:"+TimeZone.getDefault());
输出:
当前的时区:Asia/Shanghai
时区信息:sun.util.calendar.ZoneInfo[id="Asia/Shanghai",offset=28800000,dstSavings=0,useDaylight=false,transitions=19,lastRule=null]

处理不同的时区的方法:
SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
sd.setTimeZone(TimeZone.getTimeZone("GMT+8"));
String strDate = sd.format(new Date(1215782027390L));
System.out.println("正八区当前时间:"+strDate);
输出:
正八区当前时间:2008-07-11 21:13:47

 

转自:http://blog.csdn.net/nuoyan666/article/details/6658956

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值