Java中关于时间的类有两个关键的类:
java.util.Calendar;
java.util.Date;
今天在使用PostgreSQL数据库的时候,关于时间类型提供了一个timestamp with time zone,结果没注意,写入数据库的时间在页面上显示没能达到希望,问题出在该类型下的时间格式使用UTC来描述,这样要正确显示本地特征的日期就要做额外的处理。
写了几个测试,附带说明一下Java中的时间和时区
1.获取本地默认时区
1
2
3
4
|
@Test public void test1() {
System.out.println( "TimeZone.getDefault():" + TimeZone.getDefault());
}
|
默认时区内容:
1
|
TimeZone.getDefault():sun.util.calendar.ZoneInfo[id="Asia/Shanghai",offset=28800000,dstSavings=0,useDaylight=false,transitions=19,lastRule=null] |
2.支持的时区信息
1
2
3
4
5
6
7
8
|
@Test public void test3() {
String[] ids = TimeZone.getAvailableIDs();
System.out.println( "TimeZone AvailableIDs Numbers :" + ids.length);
for (String id : ids) {
System.out.println( "TimeZone:Id-" + id);
}
}
|
部分结果:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
TimeZone AvailableIDs Numbers : 619
TimeZone:Id-Etc/GMT+ 12
TimeZone:Id-Etc/GMT+ 11
TimeZone:Id-Pacific/Midway TimeZone:Id-Pacific/Niue TimeZone:Id-Pacific/Pago_Pago TimeZone:Id-Pacific/Samoa TimeZone:Id-US/Samoa TimeZone:Id-America/Adak TimeZone:Id-America/Atka TimeZone:Id-Etc/GMT+ 10
TimeZone:Id-HST TimeZone:Id-Pacific/Honolulu TimeZone:Id-Pacific/Johnston TimeZone:Id-Pacific/Rarotonga TimeZone:Id-Pacific/Tahiti TimeZone:Id-SystemV/HST10 TimeZone:Id-US/Aleutian TimeZone:Id-US/Hawaii |
3.GMT时间和本地时间显示
1
2
3
4
5
6
|
@Test public void test2() {
Date d = new Date ();
System.out.println( "Date.toGMTString():" + d.toGMTString());
System.out.println( "Date.toLocaleString():" + d.toLocaleString());
}
|
结果:
1
2
|
Date .toGMTString(): 29 Sep 2013 10 : 25 : 37 GMT
Date .toLocaleString(): 2013 - 9 - 29 18 : 25 : 37
|
4. 下面是Date对象的时间方法和Calendar对象方法对比
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
|
public void test4() {
Date d = new Date();
System.out.println( "java.util.Date:" );
System.out.println( "Date.getYear():" + d.getYear());
System.out.println( "Date.getMonth():" + d.getMonth());
System.out.println( "Date.getDay():" + d.getDay());
System.out.println( "Date.getHours():" + d.getHours());
System.out.println( "Date.getMinutes():" + d.getMinutes());
System.out.println( "Date.getSeconds():" + d.getSeconds());
}
public void test10() {
/**
* JDK1.1后:抛弃Date类,使用Calendar类
*/
Calendar c = Calendar.getInstance();
System.out.println( "YEAR:" + (c.get(Calendar.YEAR)));
System.out.println( "MONTH:" + ((c.get(Calendar.MONTH)) + 1 ));
System.out.println( "DAY_OF_MONTH:" + (c.get(Calendar.DAY_OF_MONTH)));
System.out.println( "HOUR_OF_DAY:" + (c.get(Calendar.HOUR_OF_DAY)));
System.out.println( "MINUTE:" + (c.get(Calendar.MINUTE)));
System.out.println( "SECOND:" + (c.get(Calendar.SECOND)));
System.out.println( "DAY_OF_WEEK:" + (c.get(Calendar.DAY_OF_WEEK)));
System.out.println( "DAY_OF_WEEK_IN_MONTH:"
+ (c.get(Calendar.DAY_OF_WEEK_IN_MONTH)));
System.out.println( "DAY_OF_YEAR:" + (c.get(Calendar.DAY_OF_YEAR)));
}
|
结果:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
Date类: 结果 java.util.Date: Date.getYear():113 Date.getMonth():8 Date.getDay():0 Date.getHours():18 Date.getMinutes():25 Date.getSeconds():37 Calendar类: 结果 YEAR:2013 MONTH:9 DAY_OF_MONTH:29 HOUR_OF_DAY:18 MINUTE:25 SECOND:37 DAY_OF_WEEK:1 DAY_OF_WEEK_IN_MONTH:5 DAY_OF_YEAR:272 |
Date类的大多数方法已经弃用,建议使用Calendar类,Calendar提供了功能更加完整,强大的时间处理方法和属性。
4.关于年和毫秒数
Date实例获取的year是当前年份与1900之差。
Date实例的getTime()返回当前时间距离:1970-01-01 00:00:00毫秒数。
历元(即格林威治标准时间 1970 年 1 月 1 日的 00:00:00.000,格里高利历)和1900年1月1日的00:00:00.000两个时间点是Java开发中时间日期的重要参考量。
5.UTC时间
1
2
3
4
5
6
7
|
@Test public void test5() {
long times = Date.UTC( 2 , 2 , 1 , 1 , 1 , 59 );
System.out.println( "Date.UTC(2, 2, 1, 1, 1, 59):" + times);
times = Date.UTC( 113 , 2 , 1 , 1 , 1 , 59 );
System.out.println( "Date.UTC(113, 2, 1, 1, 1, 59):" + times);
}
|
结果:
1
2
|
Date .UTC( 2 , 2 , 1 , 1 , 1 , 59 ):- 2140815481000
Date .UTC( 113 , 2 , 1 , 1 , 1 , 59 ): 1362099719000
|
这里的时间参考点就是:1970 年 1 月 1 日的 00:00:00.000
附一张时区图:
本文转自 secondriver 51CTO博客,原文链接:http://blog.51cto.com/aiilive/1303566,如需转载请自行联系原作者