package com.example.demo.controller;
import com.example.demo.util.DateUtil;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
public class Demo {
public static Date getLastDayOfMonth (Date date) {
SimpleDateFormat format = new SimpleDateFormat(DateUtil.DATE_TIME_PATTERN);
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
//获取年
int year = calendar.get(Calendar.YEAR);
//获取月份,0表示1月份
int month = calendar.get(Calendar.MONTH) + 1;
//获取本月最大天数
int lastDay = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
try {
return format.parse(year+"-"+month+"-"+lastDay + " 23:59:59");
} catch (ParseException e) {
e.printStackTrace();
}
return null;
}
public static void main(String[] args) {
Date now = new Date();
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
System.out.println("某月的最后一天23:59:59 = " + df.format(getLastDayOfMonth(now)));
}
}
Java日期——获取某月的最后一天23:59:59秒
最新推荐文章于 2024-08-23 10:48:16 发布