Chinaunix首页 | 论坛 | 博客
  • 博客访问: 517987
  • 博文数量: 260
  • 博客积分: 10435
  • 博客等级: 上将
  • 技术积分: 1939
  • 用 户 组: 普通用户
  • 注册时间: 2009-11-24 14:50
文章分类

全部博文(260)

文章存档

2011年(22)

2010年(209)

2009年(29)

我的朋友

分类: Java

2011-01-18 17:32:43

java2平台为我们提供了丰富的日期时间API。如java.util.Date;java.util.calendar;java.text.DateFormat等。那么它们之间有什么关系呢?

首先,java.util.Date代表一个时间点,其值为距公元1970年1月1日 00:00:00的毫秒数。所以它是没有时区和Locale概念的。java通过如下形式取得当前时间点:

Date now = new Date();  //这个时间点与本地系统的时区无关

而正因为其与时区的无关性,才使得我们的存储数据(时间)是一致的(时区一致性)。一般的我们将now存储于数据库中,当我们需要展现数据时,将 now格式化成想要的格式,如:2009-11-19 14:12:23。而这个功能一般交由java.text.DateFormat来实现。例如:

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String snow = sdf.format(now);  // 2009-11-19 14:12:23

我们发现snow是带时间(14:12:23)的字符串,我们不禁要问,该时间(14:12:23)是哪个时区的时间?默认情况 下,SimpleDateFormat 取得本地系统的时区(我的时区为GMT+8北京),然后按照pattern("yyyy-MM-dd HH:mm:ss")格式化now,此时输出的就是GMT+8区的时间了。如果想支持国际化时间,则先指定时区,然后再格式化date数据。例如:

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

sdf.setTimeZone(TimeZone.getTimeZone("GMT+8"));
String snow = sdf.format(now);  // 2009-11-19 14:12:23

另外,你可以通过如下代码修改本地时区信息:

TimeZone.setDefault(TimeZone.getTimeZone("GMT+8"));

 

java.util.Calendar类也代表时间点,但它为Date的facade工具类,提供了很多对时间点到年、月、日、时、分、秒、星期等的转换(计算)的方便方法。

Calendar calendar = Calendar.getInstance(timezone); 

Date d = calendar.getTime();

Calendar 的计算也是基于时区的,例如:同一个date在不同时区下的小时数是不一样的。但是calendar.getTime();返回的date是没有时区的,因为它是Date类型的。例如:

 public static void main(String[] args) throws InterruptedException {
  Calendar calendar1 = Calendar
    .getInstance(TimeZone.getTimeZone("GMT+8"));
  Calendar calendar2 = Calendar
    .getInstance(TimeZone.getTimeZone("GMT+1"));

  System.out.println("Millis = " + calendar1.getTimeInMillis());
  System.out.println("Millis = " + calendar2.getTimeInMillis());

  System.out.println("hour = " + calendar1.get(Calendar.HOUR));
  System.out.println("hour = " + calendar2.get(Calendar.HOUR));

  System.out.println("date = " + calendar1.getTime());
  System.out.println("date = " + calendar2.getTime());
 }
输出:

Millis = 1258614681203
Millis = 1258614681203
hour = 3
hour = 8
date = Thu Nov 19 15:11:21 CST 2009
date = Thu Nov 19 15:11:21 CST 2009

 


package com.taobao;

import java.util.Date;
import java.util.TimeZone;
public class Test3 {
  @SuppressWarnings("unused")
  public static void main(String[] args) throws InterruptedException {
    DateThread thread = new DateThread();
    thread.start();
       
    Date now = new Date();
    System.out.println("main thread,before set timezone:" + now.toString());
    
    Thread.currentThread().sleep(2000);
    thread.resume();
    System.out.println("main thread,after set timezone:" + now.toString());
   
  }
}
class DateThread extends Thread {
  @SuppressWarnings( { "unused", "deprecation" })
  public void run() {
      TimeZone gmt = TimeZone.getTimeZone("GMT");
        TimeZone.setDefault(gmt);
    Date now = new Date();
    System.out.println("sub thread,before set timezone:" + now.toString());
    this.suspend();
    System.out.println("sub thread,after set timezone:" + now.toString());
  }
}




import java.util.Date;
import java.util.TimeZone;

Date now = new Date();
out.println("main thread,before set timezone:" + now.toString());
TimeZone gmt = TimeZone.getTimeZone("GMT");
TimeZone.setDefault(gmt);
out.println("main thread,after set timezone:" + now.toString());


阅读(1546) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~