Java中格式化String value="Wed Sep 16 19:02:36 CST 2009";

import java.text.ParseException;
import java.text.SimpleDateFormat;

public class DataFormat {

//目的是将字符串Tue Apr 15 11:23:55 CST 2014  格式化为:2014-04-16
    public static void main(String[] args) {


        //结果为:2014-04-16 01:23:55
        /*String dateString  = "Tue Apr 15 11:23:55 CST 2014";
        SimpleDateFormat sfEnd = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        SimpleDateFormat sfStart = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy",java.util.Locale.ENGLISH) ;
        try {
            System.out.println(sfEnd.format(sfStart.parse(dateString)));
        } catch (ParseException e) {
            e.printStackTrace();
        }*/

        //结果为:2014-04-16
        String dateString  = "Tue Apr 15 11:23:55 CST 2014";
        SimpleDateFormat sfEnd = new SimpleDateFormat("yyyy-MM-dd");
        SimpleDateFormat sfStart = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy",java.util.Locale.ENGLISH) ;
        try {
            System.out.println(sfEnd.format(sfStart.parse(dateString)));
        } catch (ParseException e) {
            e.printStackTrace();
        }

    }







}

你可能感兴趣的:(java基础,java,data)