private static Map<String,String> getMonthStartAndEnd(Integer year,Integer month){
SimpleDateFormat format = new SimpleDateFormat(DateUtils.DATETIME_PATTERN_DATE);
//获取前月的第一天
Calendar cal_1=Calendar.getInstance();//获取当前日期
cal_1.set(Calendar.YEAR,year);
cal_1.set(Calendar.MONTH,month-1);
cal_1.set(Calendar.DAY_OF_MONTH,1);//设置为1号,当前日期既为本月第一天
String firstDay = format.format(cal_1.getTime());
firstDay += " 00:00:00";
//获取前月的最后一天
Calendar cale = Calendar.getInstance();
cale.set(Calendar.YEAR,year);
cale.set(Calendar.MONTH,month);
cale.set(Calendar.DAY_OF_MONTH,0);
String lastDay = format.format(cale.getTime());
lastDay += " 23:59:59";
Map<String, String> firstAndLast = new HashMap<>();
firstAndLast.put("firstDay",firstDay);
firstAndLast.put("lastDay",lastDay);
return firstAndLast;
}
获取某年某月的月初和月末时间
最新推荐文章于 2024-07-26 15:58:33 发布
本文介绍了一段Java代码,用于获取特定年份和月份的开始和结束日期,包括如何使用Calendar和SimpleDateFormat来精确到秒。
3624

被折叠的 条评论
为什么被折叠?



