CST时间格式的转换

中央标准时间(CST)转换成普通时间格式代码如下:

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Locale;

/**
 * CST时间格式转换
 *
 * @author yaohucaizi
 */
public class CSTFormat {

    public static void main(String[] args) throws ParseException {
        String s = "Thu Jul 22 23:58:32 CST 2013";
        DateFormat df = new SimpleDateFormat("EEE MMM dd HH:mm:ss 'CST' yyyy", Locale.US);
        DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        System.out.println(sdf.format(df.parse(s)));
    }
}

GMT与CST时间的转换:

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import java.util.TimeZone;

/**
 *
 * @author yaohucaizi
 */
public class NewClass {
    
    public static void main(String[] args) throws ParseException {
        Date date = new Date();
        DateFormat cstFormat = new SimpleDateFormat();
        DateFormat gmtFormat = new SimpleDateFormat();
        TimeZone gmtTime = TimeZone.getTimeZone("GMT");
        TimeZone cstTime = TimeZone.getTimeZone("CST");
        cstFormat.setTimeZone(gmtTime);
        gmtFormat.setTimeZone(cstTime);
        System.out.println("GMT Time: " + cstFormat.format(date));
        System.out.println("CST Time: " + gmtFormat.format(date));
        System.out.println(new NewClass().getGMT(date));
        System.out.println(new NewClass().getCST(new NewClass().getGMT(date)));
    }
    
    public Date getCST(String strGMT) throws ParseException {
        DateFormat df = new SimpleDateFormat("EEE, d-MMM-yyyy HH:mm:ss z", Locale.ENGLISH);
        return df.parse(strGMT);
    }
    
    public String getGMT(Date dateCST) {
        DateFormat df = new SimpleDateFormat("EEE, d-MMM-yyyy HH:mm:ss z", Locale.ENGLISH);
        df.setTimeZone(TimeZone.getTimeZone("GMT")); // modify Time Zone. 
        return (df.format(dateCST));
    }
}



  • 5
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值