Mon Dec 03 09:40:40 CST 2012 转时间格式

package hello;

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

public class TC {

    /**
     * @param args
     */
    public static void main(String[] args) {
        String str = "Mon Dec 03 09:40:40 CST 2012";
        Date date = parse(str, "EEE MMM dd HH:mm:ss zzz yyyy", Locale.US);
        System.out.printf("%tF %<tT%n", date);
    }

    public static Date parse(String str, String pattern, Locale locale) {
        if (str == null || pattern == null) {
            return null;
        }
        try {
            return new SimpleDateFormat(pattern, locale).parse(str);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static String format(Date date, String pattern, Locale locale) {
        if (date == null || pattern == null) {
            return null;
        }
        return new SimpleDateFormat(pattern, locale).format(date);
    }

}

posted on 2012-12-03 11:17  雨渐渐  阅读(228)  评论(0编辑  收藏  举报

导航