Date和String转换

package com.jpa.demo.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class DateUtil {

    private static String pattern1 = "yyyy-MM-dd";
    private static String pattern2 = "yyyy-MM-dd HH:mm:ss";

    /**
     * 日期转字符串(格式化)
     * @param pattern
     */
    public static String dateToStr(String pattern){
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat(pattern);
        String str = sdf.format(date);
        return str;
    }


    /**
     * 字符串转日期(解析)
     * @param dateStr
     * @param pattern
     */
    public static Date strToDate(String dateStr, String pattern) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat(pattern);
        Date date = sdf.parse(dateStr);
        return date;
    }
}

 

posted @ 2019-10-17 13:35  Peter.Jones  阅读(155)  评论(0编辑  收藏  举报