public class DateUtil {
/*
* 把日期格式化成字符串,传入的format为要把日期格式化成字符串的格式,例如:"yyyy-MM-dd HH:mm:ss"
*/
public static String formatDate(Date date,String format){
String result="";
SimpleDateFormat sdf=new SimpleDateFormat(format);
if(date!=null){
result=sdf.format(date);
}
return result;
}
/*
* String类型转化成日期对象,把字符串str格式化成format类型的日期
*/
public static Date formatString(String str,String format) throws Exception{
if(StringUtil.isEmpty(str)){
return null;
}
SimpleDateFormat sdf=new SimpleDateFormat(format);
//解析字符串的文本,生成 Date,并返回
return sdf.parse(str);
}
public static String getCurrentDateStr()throws Exception{
Date date=new Date();
SimpleDateFormat sdf=new SimpleDateFormat("yyyyMMddhhmmss");
return sdf.format(date);
}
}
格式化日期
最新推荐文章于 2022-01-04 14:12:47 发布