public class DateTimeUtil {
public static String DEFAULT_FORMAT = "yyyy-MM-dd";
public static Integer getLastYear() {
SimpleDateFormat formats = new SimpleDateFormat("yyyy");
Calendar c = Calendar.getInstance();
c.add(Calendar.YEAR, -1);
String format = formats.format(date);
int parseInt = Integer.parseInt(format);
log.info("去年为{}年", parseInt);
public static String formatDate(Date date) {
SimpleDateFormat f = new SimpleDateFormat(DEFAULT_FORMAT);
public static Date getCurrYearFirst() {
Calendar currCal = Calendar.getInstance();
int currentYear = currCal.get(Calendar.YEAR);
return getYearFirst(currentYear);
public static Date getCurrYearLast() {
Calendar currCal = Calendar.getInstance();
int currentYear = currCal.get(Calendar.YEAR);
return getYearLast(currentYear);
public static Date getYearFirst(int year) {
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.YEAR, year);
Date currYearFirst = calendar.getTime();
String nowTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(currYearFirst);
log.info("去年第一天为{}", nowTime);
public static Date getYearLast(int year) {
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.YEAR, year);
calendar.roll(Calendar.DAY_OF_YEAR, -1);
return calendar.getTime();
public static Long getDailyStartTime(Long timeStamp, String timeZone) {
Calendar calendar = Calendar.getInstance();
calendar.setTimeZone(TimeZone.getTimeZone(timeZone));
calendar.setTimeInMillis(timeStamp);
calendar.set(Calendar.HOUR_OF_DAY, 0);
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.MILLISECOND, 0);
return calendar.getTimeInMillis();
public static Long getDailyEndTime(Long timeStamp, String timeZone) {
Calendar calendar = Calendar.getInstance();
calendar.setTimeZone(TimeZone.getTimeZone(timeZone));
calendar.setTimeInMillis(timeStamp);
calendar.set(Calendar.HOUR_OF_DAY, 23);
calendar.set(Calendar.MINUTE, 59);
calendar.set(Calendar.SECOND, 59);
calendar.set(Calendar.MILLISECOND, 999);
return calendar.getTimeInMillis();
public static Long getMonthStartTime(Long timeStamp, String timeZone) {
Calendar calendar = Calendar.getInstance();// 获取当前日期
calendar.setTimeZone(TimeZone.getTimeZone(timeZone));
calendar.setTimeInMillis(timeStamp);
calendar.add(Calendar.YEAR, 0);
calendar.add(Calendar.MONTH, 0);
calendar.set(Calendar.DAY_OF_MONTH, 1);// 设置为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.getTimeInMillis();
public static Long getMonthEndTime(Long timeStamp, String timeZone) {
Calendar calendar = Calendar.getInstance();// 获取当前日期
calendar.setTimeZone(TimeZone.getTimeZone(timeZone));
calendar.setTimeInMillis(timeStamp);
calendar.add(Calendar.YEAR, 0);
calendar.add(Calendar.MONTH, 0);
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.getTimeInMillis();
public static Long getYearStartTime(Long timeStamp) {
Calendar calendar = Calendar.getInstance();// 获取当前日期
calendar.setTimeZone(TimeZone.getTimeZone("GMT+8:00"));
calendar.setTimeInMillis(timeStamp);
calendar.add(Calendar.YEAR, 0);
calendar.add(Calendar.DATE, 0);
calendar.add(Calendar.MONTH, 0);
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.getTimeInMillis();
public static Long getYearEndTime(Long timeStamp) {
Calendar calendar = Calendar.getInstance();// 获取当前日期
calendar.setTimeZone(TimeZone.getTimeZone("GMT+8:00"));
calendar.setTimeInMillis(timeStamp);
int year = calendar.get(Calendar.YEAR);
calendar.set(Calendar.YEAR, year);
calendar.set(Calendar.HOUR_OF_DAY, 23);
calendar.set(Calendar.MINUTE, 59);
calendar.set(Calendar.SECOND, 59);
calendar.set(Calendar.MILLISECOND, 999);
calendar.roll(Calendar.DAY_OF_YEAR, -1);
return calendar.getTimeInMillis();
public static void main(String[] args) {
Date yearFirst = getYearFirst(2023);
System.out.println("今年第一天:" + yearFirst.getTime());
Date yearLast = getYearLast(2023);
System.out.println("今年最后一天:" + yearLast.getTime());
LocalDateTime todayStart = LocalDateTime.of(LocalDate.now(), LocalTime.MIN);
String todayStartStr = todayStart.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
LocalDateTime todayEnd = LocalDateTime.of(LocalDate.now(), LocalTime.MAX);
String todayEndStr = todayEnd.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));