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;
    }

 

posted @ 2015-02-01 21:29  德玛西亚冲锋  阅读(457)  评论(0编辑  收藏  举报