#
whycq
2025-02-07 8e6d7c8275117ca2659e7f82051f8af19741aa9d
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
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);
    }
}