package com.swift.oa; import java.text.SimpleDateFormat; import java.util.Calendar; public class APP4 { public static void main(String[] args) { // TODO Auto-generated method stub String firstDay = getFirstDay(2022,12,"yyyy-MM-dd"); String lastDay = getLastDay(2022,12,"yyyy-MM-dd"); System.out.println(firstDay); System.out.println(lastDay); } /** * 根据年月获取月初第一天日期 * @param year * @param month * @return */ public static String getFirstDay(int year,int month,String format) { Calendar cale = Calendar.getInstance(); cale.set(Calendar.YEAR,year); //赋值年份 cale.set(Calendar.MONTH, month-1);//赋值月份 int lastDay = cale.getActualMinimum(Calendar.DAY_OF_MONTH);//获取月最大天数 cale.set(Calendar.DAY_OF_MONTH, lastDay);//设置日历中月份的最大天数 SimpleDateFormat sdf = new SimpleDateFormat(format);//格式化日期yyyy-MM-dd String lastDayOfMonth = sdf.format(cale.getTime()); return lastDayOfMonth; } /** * 根据年月获取月末最后一天日期 * @param year * @param month * @return */ public static String getLastDay(int year,int month,String format) { Calendar cale = Calendar.getInstance(); cale.set(Calendar.YEAR,year);//赋值年份 cale.set(Calendar.MONTH, month-1);//赋值月份 int lastDay = cale.getActualMaximum(Calendar.DAY_OF_MONTH);//获取月最大天数 cale.set(Calendar.DAY_OF_MONTH, lastDay);//设置日历中月份的最大天数 SimpleDateFormat sdf = new SimpleDateFormat(format); //格式化日期yyyy-MM-dd String lastDayOfMonth = sdf.format(cale.getTime()); return lastDayOfMonth; } }
标签:set,JAVA,String,获取,int,month,月末,Calendar,cale From: https://www.cnblogs.com/hua900822/p/16998543.html