且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在Android中将字符串转换为日期格式

更新时间:2022-11-19 08:56:59

public class Utils {

    public static void main(String[] args) {

        String mytime="Jan 17, 2012";
        SimpleDateFormat dateFormat = new SimpleDateFormat(
                "MMM dd, yyyy");

        Date myDate = null;
        try {
            myDate = dateFormat.parse(mytime);

        } catch (ParseException e) {
            e.printStackTrace();
        }

        SimpleDateFormat timeFormat = new SimpleDateFormat("yyyy-MM-dd");
        String finalDate = timeFormat.format(myDate);

        System.out.println(finalDate);
    }
}