You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
gym/DateUtil.java

25 lines
1012 B

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

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);
}
}