Java 日期字符串与日期类型转换

1、SimpleDateFormat.format 把日期类型转化到指定格式字符串

public static String convToString(Calendar cld,String template){
        String resultString=null;
        try {
            Date date=cld.getTime();
            SimpleDateFormat sdf=new SimpleDateFormat(template,Locale.getDefault());
            resultString=sdf.format(date);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return resultString;
    }

2、SimpleDateFormat.parse 把指定格式字符串转日期类型

public static Calendar convToCalender(String str,String template){

        SimpleDateFormat sdf;
        Date date;
        Calendar cltResult = Calendar.getInstance();

        sdf = new SimpleDateFormat(template, Locale.getDefault());
        try {
            date = sdf.parse(str);

            cltResult.setTime(date);

        } catch (Exception ex) {
            // TODO Auto-generated catch block
            ex.printStackTrace();
        }

        return cltResult;
    }
原文地址:https://www.cnblogs.com/Dylanblogs/p/4266137.html