日期:2014-05-20  浏览次数:20853 次

字符串“Mon Dec 31 00:00:00 CST 2012”转换成日期问题?
Mon Dec 31 00:00:00 CST 2012 这种格式的字符串怎么转换成yyyy-mm-dd日期呢?

------解决方案--------------------
Java code
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

public class Test {

    public static void main(String[] args) {
        String str = "Mon Dec 31 00:00:00 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);
    }
}