Java獲取上月份最后一天日期8位的示例代碼
先給大家介紹下Java獲取上月份最后一天日期8位。
代碼如下所示:
/**
* 獲取上個月的最后一天23點59分59秒的時間
*/
private String getBeforeLastMonthdate()throws Exception{
SimpleDateFormat sf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Calendar calendar=Calendar.getInstance();
int month=calendar.get(Calendar.MONTH);
calendar.set(Calendar.MONTH, month-1);
calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
//將小時至23
calendar.set(Calendar.HOUR_OF_DAY, 23);
//將分鐘至59
calendar.set(Calendar.MINUTE, 59);
//將秒至59
calendar.set(Calendar.SECOND,59);
String format = sf.format(calendar.getTime());
return format;
}/**
* 獲取上一個月1號0點0分0秒的時間
*/
private String getBeforeFirstMonthdate()throws Exception{
SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Calendar calendar=Calendar.getInstance();
calendar.add(Calendar.MONTH, -1);
calendar.set(Calendar.DAY_OF_MONTH, 1);
//將小時至23
calendar.set(Calendar.HOUR_OF_DAY, 23);
//將分鐘至59
calendar.set(Calendar.MINUTE, 59);
//將秒至59
calendar.set(Calendar.SECOND,59);
String format1 = format.format(calendar.getTime());
return format1;
}打印出來結(jié)果是:
上個月第一天:2022-06-01 00:00:00
上個月最后一天:2022-06-30 23:59:59
擴展:java獲取某日期上個月最后一天
Calendar c = Calendar.getInstance();
//當(dāng)前日期設(shè)置為指定日期
c.setTime(new Date());
//指定日期月份減去一
c.add(Calendar.MONTH, -1);
//指定日期月份減去一后的 最大天數(shù)
c.set(Calendar.DATE, c.getActualMaximum(Calendar.DATE));
//獲取上給月最后一天的日期
Date lastDateOfPrevMonth = c.getTime();
到此這篇關(guān)于Java獲取上月份最后一天日期8位的文章就介紹到這了,更多相關(guān)java獲取上月份日期內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java自定義協(xié)議報文封裝 添加Crc32校驗的實例
下面小編就為大家分享一篇Java自定義協(xié)議報文封裝 添加Crc32校驗的實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-01-01
Spring Data JPA 建立表的聯(lián)合主鍵
這篇文章主要介紹了Spring Data JPA 建立表的聯(lián)合主鍵。本文詳細的介紹了2種方式,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2019-04-04
java實現(xiàn)解析json復(fù)雜數(shù)據(jù)的方法詳解
這篇文章主要為大家詳細介紹了java如何實現(xiàn)解析json復(fù)雜數(shù)據(jù),文中的示例代碼講解詳細,具有一定的借鑒價值,感興趣的小伙伴可以學(xué)習(xí)一下2024-01-01
Java?3年面試經(jīng)驗告訴你Mybatis是如何進行分頁的
這篇文章主要介紹了Java?3年面試經(jīng)驗告訴你Mybatis是如何進行分頁的,對于任何ORM框架,分頁的實現(xiàn)邏輯無外乎兩種,不管怎么包裝,最終給到開發(fā)者的,只是使用上的差異而已,本文給大家講解的很明白,感興趣的朋友一起看看吧2022-09-09
Spring聲明式事務(wù)@Transactional知識點分享
在本篇文章里小編給大家整理了關(guān)于Spring聲明式事務(wù)@Transactional詳解內(nèi)容,需要的朋友們可以參考下。2020-02-02

