|
|
|
@ -0,0 +1,25 @@
|
|
|
|
|
package com.utils; // 声明包名为com.utils
|
|
|
|
|
|
|
|
|
|
// 导入用于格式化日期的Format类
|
|
|
|
|
import java.text.Format;
|
|
|
|
|
// 导入用于格式化日期的SimpleDateFormat类
|
|
|
|
|
import java.text.SimpleDateFormat;
|
|
|
|
|
// 导入用于表示日期的Date类
|
|
|
|
|
import java.util.Date;
|
|
|
|
|
|
|
|
|
|
// 定义日期工具类DateUtil
|
|
|
|
|
public class DateUtil {
|
|
|
|
|
// 定义静态方法convertString,将Date类型的日期转换为指定格式的字符串
|
|
|
|
|
// date为要转换的日期对象,format为目标日期格式的字符串
|
|
|
|
|
public static String convertString(Date date, String format) {
|
|
|
|
|
// 判断日期对象是否为空
|
|
|
|
|
if (date == null) {
|
|
|
|
|
// 如果为空,直接返回null
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
// 创建SimpleDateFormat对象,使用传入的格式字符串进行初始化
|
|
|
|
|
SimpleDateFormat sdf = new SimpleDateFormat(format);
|
|
|
|
|
// 使用SimpleDateFormat对象将日期格式化为字符串并返回
|
|
|
|
|
return sdf.format(date);
|
|
|
|
|
}
|
|
|
|
|
}
|