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