import org.apache.commons.lang.StringUtils;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
/**
* @author YH
* @ClassName DateUtil.java
* @Description TODO 时间工具类
* @createTime 2023年03月08日 09:20:00
*/
public class DateUtil {
public static String YYYY_MM_DD_HH_MM_SS = "yyyy-MM-dd HH:mm:ss";
public static String YYYY_MM_DD = "yyyy-MM-dd";
public static String HH_MM_SS = "HH:mm:ss";
/**
* 获取距离当前时间最近的 整5分钟区间 数据
* @param n 第几个区间
* @return
*/
public static String GetNearestN5MinutesDateByCurrentDate(int n) {
/**
* 日期字符串格式 :"2013-05-01 00:01:08"
*/
//获取当前时间
Calendar calendar = Calendar.getInstance();
// 得到分钟
int minute = calendar.get(Calendar.MINUTE);
int count = 0;
//时间不为00,5,10,15
if (minute != 0 && minute != 5 && minute != 10 && minute != 15
&& minute != 20 && minute != 25 && minute != 30 && minute != 35
&& minute != 40 && minute != 45 && minute != 50 && minute != 55) {
//判断距离最近的00,15,30,45的分钟数
for (int i = minute; i >= 0; i--) {
if (i % 5 == 0) {
break;
} else {
count++;
}
}
}
//获取最近的分钟
calendar.add(Calendar.MINUTE, -count - (n - 1) * 5);
//得到年/月/天/小时/分钟
int year = calendar.get(Calendar.YEAR);
int month = calendar.get(Calendar.MONTH) + 1;
int day = calendar.get(Calendar.DAY_OF_MONTH);
int hour = calendar.get(Calendar.HOUR_OF_DAY);
int min = calendar.get(Calendar.MINUTE);
StringBuffer datebuffer = new StringBuffer();
datebuffer.append(year);
datebuffer.append("-");
if (month < 10) {
datebuffer.append("0" + month + "-");
} else {
datebuffer.append(month + "-");
}
if (day < 10) {
datebuffer.append("0" + day + " ");
} else {
datebuffer.append(day + " ");
}
if (hour < 10) {
datebuffer.append("0" + hour + ":");
} else {
datebuffer.append(hour + ":");
}
if (min < 10) {
datebuffer.append("0" + min + ":");
} else {
datebuffer.append(min + ":");
}
datebuffer.append("00");
System.out.println(datebuffer.toString());
return datebuffer.toString();
}
/**
* 按日间隔时间
* @param dateStr 入参格式yyyy-MM-dd HH:mm:ss
* @param interval 间隔时间 天数
* @return
*/
public static String intervalTimeDay(String dateStr, int interval) {
SimpleDateFormat sdf = new SimpleDateFormat(YYYY_MM_DD_HH_MM_SS);
try {
Date parse = sdf.parse(dateStr);
Calendar ca = Calendar.getInstance();
ca.setTime(parse);
ca.add(Calendar.DAY_OF_MONTH, interval);
return sdf.format(ca.getTime());
} catch (ParseException e) {
e.printStackTrace();
return null;
}
}
/**
* 按小时间隔时间
* @param dateStr 入参格式yyyy-MM-dd HH:mm:ss
* @param interval 间隔时间 小时
* @return
*/
public static String intervalTimeHour(String dateStr, int interval) {
SimpleDateFormat sdf = new SimpleDateFormat(YYYY_MM_DD_HH_MM_SS);
try {
Date parse = sdf.parse(dateStr);
Calendar ca = Calendar.getInstance();
ca.setTime(parse);
ca.add(Calendar.HOUR_OF_DAY, interval);
return sdf.format(ca.getTime());
} catch (ParseException e) {
e.printStackTrace();
return null;
}
}
/**
* 按日间隔时间
*
* @return
*/
public static String intervalTimeDay(Date date, int interval, String pattern) {
if (StringUtils.isEmpty(pattern)) {
pattern = YYYY_MM_DD_HH_MM_SS;
}
SimpleDateFormat sdf = new SimpleDateFormat(pattern);
Calendar ca = Calendar.getInstance();
ca.setTime(date);
ca.add(Calendar.DAY_OF_MONTH, interval);
return sdf.format(ca.getTime());
}
public static String intervalTimeHour(Date date, int interval) {
SimpleDateFormat sdf = new SimpleDateFormat(YYYY_MM_DD_HH_MM_SS);
Calendar ca = Calendar.getInstance();
ca.setTime(date);
ca.add(Calendar.HOUR_OF_DAY, interval);
return sdf.format(ca.getTime());
}
/**
* String转Date
*
* @param dateStr
* @return
*/
public static Date strToDate(String dateStr, String pattern) {
if (StringUtils.isEmpty(pattern)) {
pattern = YYYY_MM_DD_HH_MM_SS;
}
SimpleDateFormat sdf = new SimpleDateFormat(pattern);
try {
return sdf.parse(dateStr);
} catch (ParseException e) {
e.printStackTrace();
return null;
}
}
/**
* String转Date
*
* @param date
* @return
*/
public static String dateToStr(Date date, String pattern) {
if (StringUtils.isEmpty(pattern)) {
pattern = YYYY_MM_DD_HH_MM_SS;
}
SimpleDateFormat sdf = new SimpleDateFormat(pattern);
return sdf.format(date);
}
/**
* String转LocalDate
*
* @param dateStr
* @return
*/
public static LocalDate strToLocalDate(String dateStr) {
DateTimeFormatter df = DateTimeFormatter.ofPattern(YYYY_MM_DD);
return LocalDate.parse(dateStr, df);
}
/**
* LocalDate 转 String
*
* @param localDate
* @param pattern
* @return
*/
public static String localDateToStr(LocalDate localDate, String pattern) {
if (StringUtils.isEmpty(pattern)) {
pattern = YYYY_MM_DD;
}
DateTimeFormatter sdf = DateTimeFormatter.ofPattern(pattern);
return sdf.format(localDate);
}
/**
* String转LocalDateTime
*
* @param dateStr
* @return
*/
public static LocalDateTime strToLocalDateTime(String dateStr) {
DateTimeFormatter df = DateTimeFormatter.ofPattern(YYYY_MM_DD_HH_MM_SS);
return LocalDateTime.parse(dateStr, df);
}
/**
* LocalDateTime 转 String
*
* @param localDateTime
* @param pattern
* @return
*/
public static String localDateTimeToStr(LocalDateTime localDateTime, String pattern) {
if (StringUtils.isEmpty(pattern)) {
pattern = YYYY_MM_DD_HH_MM_SS;
}
DateTimeFormatter sdf = DateTimeFormatter.ofPattern(pattern);
return sdf.format(localDateTime);
}
/**
* 获取指定日期对应的月份最后一天
*
* @param date
* @return
*/
public static String getLastMonthDay(Date date) {
//获取Calendar
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
//设置日期为本月最大日期
calendar.set(Calendar.DATE, calendar.getActualMaximum(calendar.DATE));
//设置日期格式
SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
return sf.format(calendar.getTime());
}
/**
* 获取月份天数
*
* @return
*/
public static int daysOfMonth(Date date) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
//设置日期为本月最大日期
int actualMaximum = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
return actualMaximum;
}
/**
* 获取昨日日期
* @param date
* @return
*/
public static String getYesterday(String date) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(strToDate(date, YYYY_MM_DD));
calendar.add(Calendar.DAY_OF_YEAR, -1);
Date yesterday = calendar.getTime();
return dateToStr(yesterday, YYYY_MM_DD);
}
/**
* 前X分钟数据
*
* @param time
* @param interval
* @return
*/
public static String beforeMinute(String time, String interval) {
SimpleDateFormat sd = new SimpleDateFormat(YYYY_MM_DD_HH_MM_SS); // 字符串转换成时间
if (StringUtils.isEmpty(interval)) {
//默认五分钟
interval = "5";
}
try {
Date date = sd.parse(time);
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.set(Calendar.MINUTE, (calendar.get(Calendar.MINUTE) - Integer.parseInt(interval)));
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
return df.format(calendar.getTime());
} catch (ParseException e) {
e.printStackTrace();
return null;
}
}
/**
* 获取前3天
*
* @param
* @return
*/
public static List<String> getPreviousDates(String inputDate) {
List<String> previousDates = new ArrayList<>();
LocalDate date = LocalDate.parse(inputDate, DateTimeFormatter.ISO_DATE);
for (int i = 0; i < 3; i++) {
LocalDate previousDate = date.minusDays(i + 1);
previousDates.add(previousDate.format(DateTimeFormatter.ISO_DATE));
}
return previousDates;
}
/**
* 获取前几天
*
* @return
*/
public static String dayBefore(String date, Integer day) {
String format = LocalDate.parse(date).minusDays(day).format(DateTimeFormatter.ofPattern(YYYY_MM_DD));
return format;
}
/**
* 根据月份获取天数
*
* @param calendarDay
* @return
*/
public static int getDaysOfMonth(String calendarDay) {
LocalDate date = LocalDate.parse(calendarDay + "-01");
return date.lengthOfMonth();
}
/**
* 根据月份获取天数天数
*
* @param date
* @return
*/
public static int getDayOfMonth(Date date) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
return calendar.get(Calendar.DATE);//获取日
}
public static void main(String[] args) {
}
}
DateUtil工具类
于 2023-03-08 09:37:34 首次发布