https://blog.csdn.net/itmyhome1990/article/details/85619804
Java获取指定月份的最后一天
麦田 2019-01-02 17:43:38 8036 收藏 1
展开
import java.text.SimpleDateFormat;
import java.util.Calendar;
public class DUtil {
public static String getLastDayOfMonth(String yearMonth) {
int year = Integer.parseInt(yearMonth.split("-")[0]); //年
int month = Integer.parseInt(yearMonth.split("-")[1]); //月
Calendar cal = Calendar.getInstance();
// 设置年份
cal.set(Calendar.YEAR, year);
// 设置月份
// cal.set(Calendar.MONTH, month - 1);
cal.set(Calendar.MONTH, month); //设置当前月的上一个月
// 获取某月最大天数
//int lastDay = cal.getActualMaximum(Calendar.DATE);
int lastDay = cal.getMinimum(Calendar.DATE); //获取月份中的最小值,即第一天
// 设置日历中月份的最大天数
//cal.set(Calendar.DAY_OF_MONTH, lastDay);
cal.set(Calendar.DAY_OF_MONTH, lastDay - 1); //上月的第一天减去1就是当月的最后一天
// 格式化日期
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
return sdf.format(cal.getTime());
}
public static void main(String[] args) {
System.out.println(getLastDayOfMonth("2019-02"));
}
}
1
输出:
package com.unicom.property.core.util; import com.unicom.property.common.query.DateQuery; import com.unicom.property.common.enums.StatisticsPeriodEnum; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Calendar; import java.util.Date; import java.util.HashMap; import java.util.List; import java.util.Map; /** * @Copyright: Unicom (Zhejiang) Industrial Internet Co., Ltd. 2020 <br/> * @Desc: <br/> * @ProjectName: property-charge <br/> * @Date: 2020/4/30 18:53 <br/> * @Author: yangjiabin */ public class DateSelectBoxUtil { /** * 获取指定年月的最后一天 * @param year * @param month * @return */ public static String getLastDayOfMonth1(int year, int month) { // Calendar cal = Calendar.getInstance(); // //设置年份 // cal.set(Calendar.YEAR, year); // //设置月份 // cal.set(Calendar.MONTH, month-1); // //获取某月最大天数 // int lastDay = cal.getActualMaximum(Calendar.DATE); // //设置日历中月份的最大天数 // cal.set(Calendar.DAY_OF_MONTH, lastDay); // //格式化日期 // SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); // return sdf.format(cal.getTime()); 有问题 // int year = Integer.parseInt(yearMonth.split("-")[0]); //年 // int month = Integer.parseInt(yearMonth.split("-")[1]); //月 Calendar cal = Calendar.getInstance(); // 设置年份 cal.set(Calendar.YEAR, year); // 设置月份 // cal.set(Calendar.MONTH, month - 1); cal.set(Calendar.MONTH, month); //设置当前月的上一个月 // 获取某月最大天数 //int lastDay = cal.getActualMaximum(Calendar.DATE); int lastDay = cal.getMinimum(Calendar.DATE); //获取月份中的最小值,即第一天 // 设置日历中月份的最大天数 //cal.set(Calendar.DAY_OF_MONTH, lastDay); cal.set(Calendar.DAY_OF_MONTH, lastDay - 1); //上月的第一天减去1就是当月的最后一天 // 格式化日期 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); return sdf.format(cal.getTime()); } /** * 获取指定年月的第一天 * @param year * @param month * @return */ public static String getFirstDayOfMonth1(int year, int month) { Calendar cal = Calendar.getInstance(); //设置年份 cal.set(Calendar.YEAR, year); //设置月份 cal.set(Calendar.MONTH, month-1); //获取某月最小天数 int firstDay = cal.getMinimum(Calendar.DATE); //设置日历中月份的最小天数 cal.set(Calendar.DAY_OF_MONTH,firstDay); //格式化日期 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); return sdf.format(cal.getTime()); } /** * 获取指定月第一天 * * @param day 传入2019-02 * @return */ public static String getFirstDayOfMonth2(String day) { String[] split = day.split("-"); int year = Integer.parseInt(split[0]); int month= Integer.parseInt(split[1]); String firstDayOfMonth1 = getFirstDayOfMonth1(year, month); return firstDayOfMonth1; } /** * 获取指定月份最后一天 * @param day 传入2019-02 * @return */ public static String getLastDayOfMonth2(String day) { String[] split = day.split("-"); int year = Integer.parseInt(split[0]); int month= Integer.parseInt(split[1]); String lastDayOfMonth1 = getLastDayOfMonth1(year, month); return lastDayOfMonth1; } public static Map<String, Date> getBillFinaldate(DateQuery dateQuery) { //按月 String byMonthValue = String.valueOf(StatisticsPeriodEnum.MONTH.getCode()) ; //按季度 String byQuarterlyValue = String.valueOf(StatisticsPeriodEnum.QUARTER.getCode()) ; //按半年 String byHalfYearValue = String.valueOf(StatisticsPeriodEnum.HALF_YEAR.getCode()) ; //按年 String byYearValue = String.valueOf(StatisticsPeriodEnum.YEAR.getCode()) ; Date dateStart = new Date(); Date dateEnd = new Date(); //按月 if(byMonthValue.equals(dateQuery.getSettleCycle())){ String year = dateQuery.getYear(); String month = dateQuery.getTime(); //2020-5 String yearMonth = year + "-" + month; String startime = DateSelectBoxUtil.getFirstDayOfMonth2(yearMonth) + " 00:00:00"; String endtime = DateSelectBoxUtil.getLastDayOfMonth2(yearMonth) + " 23:59:59"; //2020-5-1 00:00:00 至 2020-5-31 23:59:59 指定月份 dateStart = DateUtil.parseYyyyMMddHHmmss(startime); dateEnd= DateUtil.parseYyyyMMddHHmmss(endtime); } //按季度 if(byQuarterlyValue.equals(dateQuery.getSettleCycle())){ String year = dateQuery.getYear(); Integer time = Integer.parseInt(dateQuery.getTime()); //假设为第一季度 String startMonth = String.valueOf(3*time-2); String endMonth = String.valueOf(3*time); //2020-7 String startYearMonth = year + "-" + startMonth; //2020-9 String endYearMonth = year + "-" + endMonth; String startime = DateUtil.getFirstDayOfMonth2(startYearMonth) + " 00:00:00"; String endtime = DateUtil.getLastDayOfMonth2(endYearMonth) + " 23:59:59"; //2020-7-1 00:00:00 至 2020-9-30 23:59:59 指定月份 dateStart = DateUtil.parseYyyyMMddHHmmss(startime); dateEnd= DateUtil.parseYyyyMMddHHmmss(endtime); } //按半年 if(byHalfYearValue.equals(dateQuery.getSettleCycle())){ String year = dateQuery.getYear(); Integer time = Integer.parseInt(dateQuery.getTime()); //假设为上半年 String startMonth = String.valueOf(6*time-5); String endMonth = String.valueOf(6*time); //2020-7 String startYearMonth = year + "-" + startMonth; //2020-12 String endYearMonth = year + "-" + endMonth; String startime = DateUtil.getFirstDayOfMonth2(startYearMonth) + " 00:00:00"; String endtime = DateUtil.getLastDayOfMonth2(endYearMonth) + " 23:59:59"; //2020-7-1 00:00:00 至 2020-12-31 23:59:59 指定月份 dateStart = DateUtil.parseYyyyMMddHHmmss(startime); dateEnd= DateUtil.parseYyyyMMddHHmmss(endtime); } //按年 if(byYearValue.equals(dateQuery.getSettleCycle())){ String year = dateQuery.getYear(); //2020-5 String startime = year + "-1-1 00:00:00"; String endtime = year + "-12-31 23:59:59"; //2020-1-1 00:00:00 至 2020-12-31 23:59:59 指定月份 dateStart = DateUtil.parseYyyyMMddHHmmss(startime); dateEnd= DateUtil.parseYyyyMMddHHmmss(endtime); } Map<String,Date> map = new HashMap(); map.put("dateStart",dateStart); map.put("dateEnd",dateEnd); return map; } public static void main(String[] args) { // System.out.println("按月"); // for(int i = 1 ;i<=12;i++){ // byMounth(String.valueOf(i)); // } // System.out.println("按季度"); // for(int i = 1 ;i<=4;i++) { // byQuarter(String.valueOf(i)); // } // System.out.println("按半年"); // for(int i = 1 ;i<=2;i++) { // byHelfYear(String.valueOf(i)); // } // System.out.println("按年"); // for(int i = 1 ;i<=2;i++) { // byYear(String.valueOf(i)); // } //0-按月 1-按季度 2-按半年 3-年 DateQuery dateQuery = new DateQuery(); dateQuery.setYear("2020"); dateQuery.setSettleCycle("0"); List<Map<String, Date>> mounthList = getMounthList(dateQuery); printList(mounthList); System.out.println(); //0-按月 1-按季度 2-按半年 3-年 DateQuery dateQuery2 = new DateQuery(); dateQuery2.setYear("2020"); dateQuery2.setSettleCycle("1"); List<Map<String, Date>> mounthList2 = getQuarterList(dateQuery2); printList(mounthList2); System.out.println(); //0-按月 1-按季度 2-按半年 3-年 DateQuery dateQuery3 = new DateQuery(); dateQuery3.setYear("2020"); dateQuery3.setSettleCycle("2"); List<Map<String, Date>> mounthList3 = getHelfYearList(dateQuery3); printList(mounthList3); System.out.println(); //0-按月 1-按季度 2-按半年 3-年 DateQuery dateQuery4 = new DateQuery(); dateQuery4.setYear("2020"); dateQuery4.setSettleCycle("3"); List<Map<String, Date>> mounthList4 = getYearList(dateQuery4); printList(mounthList4); System.out.println(); } public static void printList(List<Map<String, Date>> mounthList) { System.out.println("---------------------------------------"); for(Map<String, Date> temp: mounthList){ Date dateStart = temp.get("dateStart"); Date dateEnd = temp.get("dateEnd"); System.out.println(DateUtil.formatYyyyMMddHHmmss(dateStart)+" --> "+DateUtil.formatYyyyMMddHHmmss(dateEnd)); System.out.println(); } } public static List<Map<String, Date>> getMounthList(DateQuery dateQuery) { //0-按月 1-按季度 2-按半年 3-年 System.out.println("按月"); List<Map<String, Date>> timeList= new ArrayList<>(); for(int i = 1 ;i<=12;i++){ dateQuery.setTime(String.valueOf(i)); Map<String, Date> billFinaldate = DateSelectBoxUtil.getBillFinaldate(dateQuery); timeList.add(billFinaldate); } return timeList; } public static void byMounth(String mounth) { //0-按月 1-按季度 2-按半年 3-年 DateQuery dateQuery = new DateQuery(); dateQuery.setYear("2020"); dateQuery.setSettleCycle("0"); dateQuery.setTime(mounth); Map<String, Date> billFinaldate = DateSelectBoxUtil.getBillFinaldate(dateQuery); Date dateStart = billFinaldate.get("dateStart"); Date dateEnd = billFinaldate.get("dateEnd"); System.out.println(DateUtil.formatYyyyMMddHHmmss(dateStart)+" --> "+DateUtil.formatYyyyMMddHHmmss(dateEnd)); System.out.println(); } public static List<Map<String, Date>> getQuarterList(DateQuery dateQuery) { //0-按月 1-按季度 2-按半年 3-年 System.out.println("按季度"); List<Map<String, Date>> timeList= new ArrayList<>(); for(int i = 1 ;i<=4;i++){ dateQuery.setTime(String.valueOf(i)); Map<String, Date> billFinaldate = DateSelectBoxUtil.getBillFinaldate(dateQuery); timeList.add(billFinaldate); } return timeList; } public static void byQuarter(String time) { //0-按月 1-按季度 2-按半年 3-年 DateQuery dateQuery = new DateQuery(); dateQuery.setYear("2020"); dateQuery.setSettleCycle("1"); dateQuery.setTime(time); Map<String, Date> billFinaldate = DateSelectBoxUtil.getBillFinaldate(dateQuery); Date dateStart = billFinaldate.get("dateStart"); Date dateEnd = billFinaldate.get("dateEnd"); System.out.println(DateUtil.formatYyyyMMddHHmmss(dateStart)+" --> "+DateUtil.formatYyyyMMddHHmmss(dateEnd)); System.out.println(); } public static List<Map<String, Date>> getHelfYearList(DateQuery dateQuery) { //0-按月 1-按季度 2-按半年 3-年 System.out.println("按半年"); List<Map<String, Date>> timeList= new ArrayList<>(); for(int i = 1 ;i<=2;i++){ dateQuery.setTime(String.valueOf(i)); Map<String, Date> billFinaldate = DateSelectBoxUtil.getBillFinaldate(dateQuery); timeList.add(billFinaldate); } return timeList; } public static void byHelfYear(String time) { //0-按月 1-按季度 2-按半年 3-年 DateQuery dateQuery = new DateQuery(); dateQuery.setYear("2020"); dateQuery.setSettleCycle("2"); dateQuery.setTime(time); Map<String, Date> billFinaldate = DateSelectBoxUtil.getBillFinaldate(dateQuery); Date dateStart = billFinaldate.get("dateStart"); Date dateEnd = billFinaldate.get("dateEnd"); System.out.println(DateUtil.formatYyyyMMddHHmmss(dateStart)+" --> "+DateUtil.formatYyyyMMddHHmmss(dateEnd)); System.out.println(); } public static List<Map<String, Date>> getYearList(DateQuery dateQuery) { //0-按月 1-按季度 2-按半年 3-年 System.out.println("按年"); List<Map<String, Date>> timeList= new ArrayList<>(); for(int i = 1 ;i<=1;i++){ dateQuery.setTime(String.valueOf(i)); Map<String, Date> billFinaldate = DateSelectBoxUtil.getBillFinaldate(dateQuery); timeList.add(billFinaldate); } return timeList; } public static void byYear(String time) { //0-按月 1-按季度 2-按半年 3-年 DateQuery dateQuery = new DateQuery(); dateQuery.setYear("2020"); dateQuery.setSettleCycle("3"); dateQuery.setTime(time); Map<String, Date> billFinaldate = DateSelectBoxUtil.getBillFinaldate(dateQuery); Date dateStart = billFinaldate.get("dateStart"); Date dateEnd = billFinaldate.get("dateEnd"); System.out.println(DateUtil.formatYyyyMMddHHmmss(dateStart)+" --> "+DateUtil.formatYyyyMMddHHmmss(dateEnd)); System.out.println(); } }
2019-02-28
1
PS:2月份获取的最后一天日期不对,抱歉,已改正。
修改为:用当前月的上个月的第一天减去1就是当月的最后一天。
更正:2019-11-29
作者:itmyhome
麦田 博客专家
原创文章 697获赞 2990访问量 425万+
关注
他的留言板
点赞
3
评论
10
分享
收藏
1
手机看
打赏
u013355638的博客
450
java获取时间某月的最后一天,打印昨天的当前时刻
7. Java 中的日期和时间(2017-11-19-wl) 7.1 如何取得年月日、小时分钟秒?(2017-11-19-wl) public class DateTimeTest { public...
佬油条:2月不对5个月前收起评论
码哥麦田回复佬油条:已修改,多谢指正5个月前
1
eternalsing:很强 棒棒哒6个月前
码哥神界码农-D-艾斯:用2月试了吗?9个月前收起评论
nameFnameHnameW回复神界码农-D-艾斯:
没事了,看错了,之前别人写错了,谢谢。6个月前
码哥神界码农-D-艾斯回复nameFnameHnameW:
Calendar cal = Calendar.getInstance();
cal.clear();
//code....
先clear,再做其他业务。6个月前
nameFnameHnameW回复神界码农-D-艾斯:
clear了,2月和9月也是不对啊6个月前
XV_DD回复神界码农-D-艾斯:感谢8个月前
码哥神界码农-D-艾斯回复码哥神界码农-D-艾斯:
你少写一句代码,在Calendar cal = Calendar.getInstance(); 之后,必须clear一次,否则取出的值是上一次的。9个月前
1
武中奇:超级爱你,比心10个月前
————————————————
版权声明:本文为CSDN博主「麦田」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/itmyhome1990/article/details/85619804