Date
package com.qiangesoft.utils.date;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
public class DateUtil {
public static final String[] CHINESE_WEEK_DAY = new String[]{"周一", "周二", "周三", "周四", "周五", "周六", "周日"};
public static final String FORMAT_DATE = "yyyy-MM-dd";
public static final String FORMAT_HOUR = "yyyy-MM-dd HH";
public static final String FORMAT_MINUTE = "yyyy-MM-dd HH:mm";
public static final String FORMAT_SECOND = "yyyy-MM-dd HH:mm:ss";
public static final String FORMAT_MILLISECOND = "yyyy-MM-dd HH:mm:ss:SSS";
public static final String FORMAT_DATE_NUMBER = "yyyyMMdd";
public static final String FORMAT_HOUR_NUMBER = "yyyyMMddHH";
public static final String FORMAT_MINUTE_NUMBER = "yyyyMMddHHmm";
public static final String FORMAT_SECOND_NUMBER = "yyyyMMddHHmmss";
public static final String FORMAT_MILLISECOND_NUMBER = "yyyyMMddHHmmssSSS";
public static Date now() {
return new Date();
}
public static Date startOfDay() {
return startOfDay(now());
}
public static Date startOfDay(Date date) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.set(Calendar.HOUR_OF_DAY, 0);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.MILLISECOND, 0);
return calendar.getTime();
}
public static Date endOfDay() {
return endOfDay(now());
}
public static Date endOfDay(Date date) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.set(Calendar.HOUR_OF_DAY, 23);
calendar.set(Calendar.MINUTE, 59);
calendar.set(Calendar.SECOND, 59);
calendar.set(Calendar.MILLISECOND, 999);
return calendar.getTime();
}
public static int dayOfWeek() {
return dayOfWeek(now());
}
public static int dayOfWeek(Date date) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
int dayNum = calendar.get(Calendar.DAY_OF_WEEK);
return dayNum == 1 ? 7 : dayNum - 1;
}
public static String dayNameOfWeek() {
return dayNameOfWeek(now());
}
public static String dayNameOfWeek(Date date) {
int dayNum = dayOfWeek(date);
return CHINESE_WEEK_DAY[dayNum - 1];
}
public static Date startOfWeek() {
return startOfWeek(now());
}
public static Date startOfWeek(Date date) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
calendar.set(Calendar.HOUR_OF_DAY, 0);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.MILLISECOND, 0);
return calendar.getTime();
}
public static Date endOfWeek() {
return endOfWeek(now());
}
public static Date endOfWeek(Date date) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
calendar.set(Calendar.HOUR_OF_DAY, 23);
calendar.set(Calendar.MINUTE, 59);
calendar.set(Calendar.SECOND, 59);
calendar.set(Calendar.MILLISECOND, 999);
calendar.add(Calendar.WEEK_OF_YEAR, 1);
return calendar.getTime();
}
public static Date startOfMonth() {
return startOfMonth(now());
}
public static Date startOfMonth(Date date) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.set(Calendar.DAY_OF_MONTH, 1);
calendar.set(Calendar.HOUR_OF_DAY, 0);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.MILLISECOND, 0);
return calendar.getTime();
}
public static Date endOfMonth() {
return endOfMonth(now());
}
public static Date endOfMonth(Date date) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
calendar.set(Calendar.HOUR_OF_DAY, 23);
calendar.set(Calendar.MINUTE, 59);
calendar.set(Calendar.SECOND, 59);
calendar.set(Calendar.MILLISECOND, 999);
return calendar.getTime();
}
public static Date startOfYear() {
return startOfYear(now());
}
public static Date startOfYear(Date date) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.set(Calendar.DAY_OF_YEAR, 1);
calendar.set(Calendar.HOUR_OF_DAY, 0);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.MILLISECOND, 0);
return calendar.getTime();
}
public static Date endOfYear() {
return endOfYear(now());
}
public static Date endOfYear(Date date) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.set(Calendar.DAY_OF_YEAR, calendar.getActualMaximum(Calendar.DAY_OF_YEAR));
calendar.set(Calendar.HOUR_OF_DAY, 0);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.MILLISECOND, 0);
return calendar.getTime();
}
public static Boolean between(Date startTime, Date endTime) {
return between(now(), startTime, endTime);
}
public static Boolean between(Date date, Date startTime, Date endTime) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
Calendar start = Calendar.getInstance();
start.setTime(startTime);
Calendar end = Calendar.getInstance();
end.setTime(endTime);
return calendar.after(start) && calendar.before(end);
}
public static String format(Date date) {
return format(date, FORMAT_SECOND);
}
public static String format(Date date, String format) {
SimpleDateFormat dateFormat = new SimpleDateFormat(format);
return dateFormat.format(date);
}
public static Date parse(String date, String format) throws ParseException {
SimpleDateFormat dateFormat = new SimpleDateFormat(format);
return dateFormat.parse(date);
}
public static Date addSecond(int second) {
return addSecond(now(), second);
}
public static Date addSecond(Date date, int second) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.add(Calendar.SECOND, second);
return calendar.getTime();
}
public static Date addMinute(int minute) {
return addMinute(now(), minute);
}
public static Date addMinute(Date date, int minute) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.add(Calendar.MINUTE, minute);
return calendar.getTime();
}
public static Date addHour(int hour) {
return addHour(now(), hour);
}
public static Date addHour(Date date, int hour) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.add(Calendar.HOUR, hour);
return calendar.getTime();
}
public static Date addDay(int day) {
return addDay(day);
}
public static Date addDay(Date date, int day) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.add(Calendar.DATE, day);
return calendar.getTime();
}
public static Date addWeek(int week) {
return addWeek(now(), week);
}
public static Date addWeek(Date date, int week) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.add(Calendar.WEEK_OF_YEAR, week);
return calendar.getTime();
}
public static Date addMonth(int month) {
return addMonth(now(), month);
}
public static Date addMonth(Date date, int month) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.add(Calendar.MONTH, month);
return calendar.getTime();
}
public static Date addYear(int year) {
return addYear(now(), year);
}
public static Date addYear(Date date, int year) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.add(Calendar.YEAR, year);
return calendar.getTime();
}
public static Date reduceSecond(int second) {
return reduceSecond(now(), second);
}
public static Date reduceSecond(Date date, int second) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.add(Calendar.SECOND, -second);
return calendar.getTime();
}
public static Date reduceMinute(int minute) {
return reduceMinute(now(), minute);
}
public static Date reduceMinute(Date date, int minute) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.add(Calendar.MINUTE, -minute);
return calendar.getTime();
}
public static Date reduceHour(int hour) {
return reduceHour(now(), hour);
}
public static Date reduceHour(Date date, int hour) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.add(Calendar.HOUR, -hour);
return calendar.getTime();
}
public static Date reduceDay(int day) {
return reduceDay(now(), day);
}
public static Date reduceDay(Date date, int day) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.add(Calendar.DATE, -day);
return calendar.getTime();
}
public static Date reduceWeek(int week) {
return reduceWeek(now(), week);
}
public static Date reduceWeek(Date date, int week) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.add(Calendar.WEEK_OF_YEAR, -week);
return calendar.getTime();
}
public static Date reduceMonth(int month) {
return reduceMonth(now(), month);
}
public static Date reduceMonth(Date date, int month) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.add(Calendar.MONTH, -month);
return calendar.getTime();
}
public static Date reduceYear(int year) {
return reduceYear(now(), year);
}
public static Date reduceYear(Date date, int year) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.add(Calendar.YEAR, -year);
return calendar.getTime();
}
public static void main(String[] args) throws ParseException {
Date now = now();
System.out.println("现在时间:" + format(now));
System.out.println("今天开始时间:" + format(startOfDay(now)));
System.out.println("今天结束时间:" + format(endOfDay(now)));
System.out.println("本周第几天:" + dayOfWeek(now));
System.out.println("本周星期几:" + dayNameOfWeek(now));
System.out.println("本周开始时间:" + format(startOfWeek(now)));
System.out.println("本周结束时间:" + format(endOfWeek(now)));
System.out.println("本月开始时间:" + format(startOfMonth(now)));
System.out.println("本月结束时间:" + format(endOfMonth(now)));
System.out.println("本年开始时间:" + format(startOfYear(now)));
System.out.println("本年结束时间:" + format(endOfYear(now)));
System.out.println("是否属于时间范围:" + between(reduceDay(now, 1), addDay(now, 2)));
System.out.println("时间格式化:" + format(now, FORMAT_MINUTE));
System.out.println("字符串转换时间:" + parse("2024-11-24 11:17:04", FORMAT_SECOND));
System.out.println("现在时间加100秒:" + format(addSecond(now, 100)));
System.out.println("现在时间加12分钟:" + format(addMinute(now, 12)));
System.out.println("现在时间加2小时:" + format(addHour(now, 2)));
System.out.println("现在时间加5天:" + format(addDay(now, 5)));
System.out.println("现在时间加3周:" + format(addWeek(now, 3)));
System.out.println("现在时间加6月:" + format(addMonth(now, 6)));
System.out.println("现在时间加1年:" + format(addYear(now, 1)));
System.out.println("现在时间减100秒:" + format(reduceSecond(now, 100)));
System.out.println("现在时间减12分钟:" + format(reduceMinute(now, 12)));
System.out.println("现在时间减2小时:" + format(reduceHour(now, 2)));
System.out.println("现在时间减5天:" + format(reduceDay(now, 5)));
System.out.println("现在时间减3周:" + format(reduceWeek(now, 3)));
System.out.println("现在时间减6月:" + format(reduceMonth(now, 6)));
System.out.println("现在时间减1年:" + format(reduceYear(now, 1)));
}
}
LocalDateTime
package com.qiangesoft.utils.date;
import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAdjusters;
public class LocalDateUtil {
public static final String[] CHINESE_WEEK_DAY = new String[]{"周一", "周二", "周三", "周四", "周五", "周六", "周日"};
public static final String FORMAT_DATE = "yyyy-MM-dd";
public static final String FORMAT_HOUR = "yyyy-MM-dd HH";
public static final String FORMAT_MINUTE = "yyyy-MM-dd HH:mm";
public static final String FORMAT_SECOND = "yyyy-MM-dd HH:mm:ss";
public static final String FORMAT_MILLISECOND = "yyyy-MM-dd HH:mm:ss:SSS";
public static final String FORMAT_DATE_NUMBER = "yyyyMMdd";
public static final String FORMAT_HOUR_NUMBER = "yyyyMMddHH";
public static final String FORMAT_MINUTE_NUMBER = "yyyyMMddHHmm";
public static final String FORMAT_SECOND_NUMBER = "yyyyMMddHHmmss";
public static final String FORMAT_MILLISECOND_NUMBER = "yyyyMMddHHmmssSSS";
public static LocalDateTime now() {
return LocalDateTime.now();
}
public static LocalDateTime startOfDay() {
return now().toLocalDate().atStartOfDay();
}
public static LocalDateTime startOfDay(LocalDateTime date) {
return date.toLocalDate().atStartOfDay();
}
public static LocalDateTime endOfDay() {
return now().toLocalDate().atTime(LocalTime.MAX);
}
public static LocalDateTime endOfDay(LocalDateTime date) {
return date.toLocalDate().atTime(LocalTime.MAX);
}
public static int dayOfWeek() {
return dayOfWeek(now());
}
public static int dayOfWeek(LocalDateTime date) {
DayOfWeek dayOfWeek = date.getDayOfWeek();
return dayOfWeek.getValue();
}
public static String dayNameOfWeek() {
return dayNameOfWeek(now());
}
public static String dayNameOfWeek(LocalDateTime date) {
int dayNum = dayOfWeek(date);
return CHINESE_WEEK_DAY[dayNum - 1];
}
public static LocalDateTime startOfWeek() {
return startOfWeek(now());
}
public static LocalDateTime startOfWeek(LocalDateTime date) {
return date.minusDays(date.getDayOfWeek().getValue() - 1).toLocalDate().atStartOfDay();
}
public static LocalDateTime endOfWeek() {
return endOfWeek(now());
}
public static LocalDateTime endOfWeek(LocalDateTime date) {
return LocalDateTime.of(LocalDate.from(date.plusDays(8 - date.getDayOfWeek().getValue())), LocalTime.MAX);
}
public static LocalDateTime startOfMonth() {
return startOfMonth(now());
}
public static LocalDateTime startOfMonth(LocalDateTime date) {
return date.toLocalDate().with(TemporalAdjusters.firstDayOfMonth()).atTime(LocalTime.MIN);
}
public static LocalDateTime endOfMonth() {
return endOfMonth(now());
}
public static LocalDateTime endOfMonth(LocalDateTime date) {
return date.toLocalDate().with(TemporalAdjusters.lastDayOfMonth()).atTime(LocalTime.MAX);
}
public static LocalDateTime startOfYear() {
return startOfYear(now());
}
public static LocalDateTime startOfYear(LocalDateTime date) {
return date.toLocalDate().with(TemporalAdjusters.firstDayOfYear()).atTime(LocalTime.MIN);
}
public static LocalDateTime endOfYear() {
return endOfYear(now());
}
public static LocalDateTime endOfYear(LocalDateTime date) {
return date.toLocalDate().with(TemporalAdjusters.lastDayOfYear()).atTime(LocalTime.MAX);
}
public static Boolean between(LocalDateTime startTime, LocalDateTime endTime) {
return between(now(), startTime, endTime);
}
public static Boolean between(LocalDateTime date, LocalDateTime startTime, LocalDateTime endTime) {
return date.isAfter(startTime) && date.isBefore(endTime);
}
public static String format(LocalDateTime date) {
return format(date, FORMAT_SECOND);
}
public static String format(LocalDateTime date, String format) {
DateTimeFormatter formatter = DateTimeFormatter.ofPattern(format);
return date.format(formatter);
}
public static LocalDateTime parse(String date, String format) {
DateTimeFormatter formatter = DateTimeFormatter.ofPattern(format);
return LocalDateTime.parse(date, formatter);
}
public static LocalDateTime addSecond(int second) {
return addSecond(now(), second);
}
public static LocalDateTime addSecond(LocalDateTime date, int second) {
return date.plusSeconds(second);
}
public static LocalDateTime addMinute(int minute) {
return addMinute(now(), minute);
}
public static LocalDateTime addMinute(LocalDateTime date, int minute) {
return date.plusMinutes(minute);
}
public static LocalDateTime addHour(int hour) {
return addHour(now(), hour);
}
public static LocalDateTime addHour(LocalDateTime date, int hour) {
return date.plusHours(hour);
}
public static LocalDateTime addDay(int day) {
return addDay(now(), day);
}
public static LocalDateTime addDay(LocalDateTime date, int day) {
return date.plusDays(day);
}
public static LocalDateTime addWeek(int week) {
return addWeek(now(), week);
}
public static LocalDateTime addWeek(LocalDateTime date, int week) {
return date.plusWeeks(week);
}
public static LocalDateTime addMonth(int month) {
return addMonth(now(), month);
}
public static LocalDateTime addMonth(LocalDateTime date, int month) {
return date.plusMonths(month);
}
public static LocalDateTime addYear(int year) {
return addYear(now(), year);
}
public static LocalDateTime addYear(LocalDateTime date, int year) {
return date.plusYears(year);
}
public static LocalDateTime reduceSecond(int second) {
return reduceSecond(now(), second);
}
public static LocalDateTime reduceSecond(LocalDateTime date, int second) {
return date.minusSeconds(second);
}
public static LocalDateTime reduceMinute(int minute) {
return reduceMinute(now(), minute);
}
public static LocalDateTime reduceMinute(LocalDateTime date, int minute) {
return date.minusMinutes(minute);
}
public static LocalDateTime reduceHour(int hour) {
return reduceHour(now(), hour);
}
public static LocalDateTime reduceHour(LocalDateTime date, int hour) {
return date.minusHours(hour);
}
public static LocalDateTime reduceDay(int day) {
return reduceDay(now(), day);
}
public static LocalDateTime reduceDay(LocalDateTime date, int day) {
return date.minusDays(day);
}
public static LocalDateTime reduceWeek(int week) {
return reduceWeek(now(), week);
}
public static LocalDateTime reduceWeek(LocalDateTime date, int week) {
return date.minusWeeks(week);
}
public static LocalDateTime reduceMonth(int month) {
return reduceMonth(now(), month);
}
public static LocalDateTime reduceMonth(LocalDateTime date, int month) {
return date.minusMonths(month);
}
public static LocalDateTime reduceYear(int year) {
return reduceYear(now(), year);
}
public static LocalDateTime reduceYear(LocalDateTime date, int year) {
return date.minusYears(year);
}
public static void main(String[] args) {
LocalDateTime now = LocalDateTime.now();
System.out.println("现在时间:" + format(now));
System.out.println("今天开始时间:" + format(startOfDay(now)));
System.out.println("今天结束时间:" + format(endOfDay(now)));
System.out.println("本周第几天:" + dayOfWeek(now));
System.out.println("本周星期几:" + dayNameOfWeek(now));
System.out.println("本周开始时间:" + format(startOfWeek(now)));
System.out.println("本周结束时间:" + format(endOfWeek(now)));
System.out.println("本月开始时间:" + format(startOfMonth(now)));
System.out.println("本月结束时间:" + format(endOfMonth(now)));
System.out.println("本年开始时间:" + format(startOfYear(now)));
System.out.println("本年结束时间:" + format(endOfYear(now)));
System.out.println("是否属于时间范围:" + between(reduceDay(now, 1), addDay(now, 2)));
System.out.println("时间格式化:" + format(now, FORMAT_MINUTE));
System.out.println("字符串转换时间:" + parse("2024-11-24 11:17:04", FORMAT_SECOND));
System.out.println("现在时间加100秒:" + format(addSecond(now, 100)));
System.out.println("现在时间加12分钟:" + format(addMinute(now, 12)));
System.out.println("现在时间加2小时:" + format(addHour(now, 2)));
System.out.println("现在时间加5天:" + format(addDay(now, 5)));
System.out.println("现在时间加3周:" + format(addWeek(now, 3)));
System.out.println("现在时间加6月:" + format(addMonth(now, 6)));
System.out.println("现在时间加1年:" + format(addYear(now, 1)));
System.out.println("现在时间减100秒:" + format(reduceSecond(now, 100)));
System.out.println("现在时间减12分钟:" + format(reduceMinute(now, 12)));
System.out.println("现在时间减2小时:" + format(reduceHour(now, 2)));
System.out.println("现在时间减5天:" + format(reduceDay(now, 5)));
System.out.println("现在时间减3周:" + format(reduceWeek(now, 3)));
System.out.println("现在时间减6月:" + format(reduceMonth(now, 6)));
System.out.println("现在时间减1年:" + format(reduceYear(now, 1)));
}
}