package com.dayu.pipirrapp.utils;
|
|
import java.text.SimpleDateFormat;
|
import java.util.Date;
|
import java.util.Locale;
|
|
/**
|
* DateUtils - 处理时间统一的类
|
*
|
* @author zuoxiao
|
* @version 1.0
|
* @since 2024-11-29
|
*/
|
public class DateUtils {
|
|
|
/**
|
* 返回统一格式的当前时间
|
*
|
* @return yyyy-MM-dd HH:mm:ss
|
*/
|
public static String getNowDateStr() {
|
// 当前时间
|
Date date = new Date();
|
// 创建 SimpleDateFormat 对象,设置日期格式
|
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault());
|
// 格式化当前时间为字符串
|
return sdf.format(date);
|
}
|
|
/**
|
* 返回统一格式的当前时间截止到分钟
|
*
|
* @return yyyy-MM-dd HH:mm
|
*/
|
public static String getNowDateToMMStr() {
|
// 当前时间
|
Date date = new Date();
|
// 创建 SimpleDateFormat 对象,设置日期格式
|
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm", Locale.getDefault());
|
// 格式化当前时间为字符串
|
return sdf.format(date);
|
}
|
|
/**
|
* 将时间戳转换为指定格式的字符串
|
* @param timestamp 时间戳
|
* @return 格式化后的时间字符串 (yyyy-MM-dd HH:mm)
|
*/
|
public static String formatTimestamp(long timestamp) {
|
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm", Locale.getDefault());
|
return sdf.format(new Date(timestamp));
|
}
|
|
}
|