Java获取指定月份的最后一天
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-5String 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-7String startYearMonth = year + "-" + startMonth;//2020-9String 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-7String startYearMonth = year + "-" + startMonth;//2020-12String 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-5String 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
總結
以上是生活随笔為你收集整理的Java获取指定月份的最后一天的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: excel设置时间格式(excel设置时
- 下一篇: 饕餮是辟邪的还是招财的(与貔貅有什么区别