将格林威治时间转换为本地时间

来源:互联网 发布:sql 不包含指定条件值 编辑:程序博客网 时间:2024/04/17 07:49

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.TimeZone;

/**
 * 将格林威治时间转换为本地时间
 *
 * @author            Wangqy
 * @version           1.0
 * @since             2009-9-24
 */

public class FormatTime {
    public static void main(String args[]) throws ParseException{
        DateFormat format1 = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
        Date date = format1.parse("2009-08-11T07:34:43Z");
        
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        
        GregorianCalendar ca = new GregorianCalendar(TimeZone.getTimeZone("GMT 00:00"));  
        ca.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), cal.get(Calendar.DAY_OF_MONTH),
                cal.get(Calendar.HOUR), cal.get(Calendar.MINUTE), cal.get(Calendar.SECOND));  
        
        SimpleDateFormat format=new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");  
        format.setTimeZone(TimeZone.getDefault());  
        System.out.println(format.format(ca.getTime()));
    }
}

原创粉丝点击