时间格式化工具类

package com.sec.util;

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

public class DateTimeUtil {
/**
* string类型改为Date(时、分、秒)
*
*/
public static Date convertDate(String dateStr){
if(StringUtil.isNullOrEmpty(dateStr)){
return null;
}
Date d = null;
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
try {
d = sdf.parse(dateStr);
} catch (ParseException e) {
e.printStackTrace();
}
return d;
}

/**
* 将Date改为date类型
* @param data
* @return
*/
public static String convertDate(Date date) {
if(null==date){
return "";
}
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
return sdf.format(date);
}

/**
*
* @param date
* @return
*/
public static String convertDateNoTime(Date date) {
if(null==date){
return "";
}
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
return sdf.format(date);
}
}

原文地址:https://www.cnblogs.com/bsyx/p/4129436.html