字符串与日期转换工具类

壹 | 代码

package com.itheima.ssm.utils;

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

public class DateUtils {

//    日期抓换成字符串
    public static String date2String(Date date, String patt)
    {
        SimpleDateFormat sdf = new SimpleDateFormat(patt);
        String format = sdf.format(date);
        return format;
    }

//    字符串转换成日期
    public static Date string2Date(String string, String patt) throws Exception
    {
        SimpleDateFormat sdf = new SimpleDateFormat(patt);
        Date parse = sdf.parse(string);
        return parse;
    }
}


//对应的构造方法:
    public String getDepartureTimeStr() {
        if (departureTime!=null)
        {
            departureTimeStr= DateUtils.date2String(departureTime,"yyyy-MM-dd HH:mm:ss");
        }

        return departureTimeStr;
    }

你可能感兴趣的:(工具类)