#
whycq
2025-02-07 8e6d7c8275117ca2659e7f82051f8af19741aa9d
#
1个文件已添加
50 ■■■■■ 已修改文件
app/src/main/java/com/example/agvcontroller/utils/DateUtils.java 50 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
app/src/main/java/com/example/agvcontroller/utils/DateUtils.java
New file
@@ -0,0 +1,50 @@
package com.example.agvcontroller.utils;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
public class DateUtils {
    /**
     * 获取当前时间的默认格式化字符串(格式:yyyy-MM-dd HH:mm:ss)
     *
     * @return 格式化后的时间字符串
     */
    public static String getCurrentTime() {
        return formatDate(new Date(), "yyyy-MM-dd HH:mm:ss");
    }
    /**
     * 根据指定格式获取当前时间的格式化字符串
     *
     * @param pattern 时间格式(例如:yyyy-MM-dd HH:mm:ss)
     * @return 格式化后的时间字符串
     */
    public static String getCurrentTime(String pattern) {
        return formatDate(new Date(), pattern);
    }
    /**
     * 格式化日期对象为字符串
     *
     * @param date    日期对象
     * @param pattern 时间格式(例如:yyyy-MM-dd HH:mm:ss)
     * @return 格式化后的时间字符串
     */
    public static String formatDate(Date date, String pattern) {
        SimpleDateFormat dateFormat = new SimpleDateFormat(pattern, Locale.getDefault());
        return dateFormat.format(date);
    }
    /**
     * 将时间戳转换为格式化字符串
     *
     * @param timestamp 时间戳(毫秒)
     * @param pattern   时间格式(例如:yyyy-MM-dd HH:mm:ss)
     * @return 格式化后的时间字符串
     */
    public static String formatTimestamp(long timestamp, String pattern) {
        return formatDate(new Date(timestamp), pattern);
    }
}