Java 獲取上一個月的月份的正確寫法
更新時間:2023年09月05日 10:53:01 作者:Best_Liu~
這篇文章主要介紹了java獲取上一個月月份,本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
Java 獲取上一個月的月份的正確寫法
因最近在寫代碼的時候遇到了獲取上個月月份的問題yyyy-MM這個格式,根據給的工具類,獲取出來的值是有問題的,所以記錄以下。
問題方法
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM"); Date date = new Date(); System.out.println(nowSdf.format(date)); Calendar calendar = Calendar.getInstance(); // 設置為當前時間 calendar.setTime(date); // 設置為上一個月 calendar.set(Calendar.MONTH, calendar.get(Calendar.MONTH) - 1); date = calendar.getTime();
正確的方法
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM"); Date date = new Date(); System.out.println(nowSdf.format(date)); Calendar calendar = Calendar.getInstance(); // 設置為當前時間 calendar.setTime(date); // 設置為上一個月 //calendar.set(Calendar.MONTH, calendar.get(Calendar.MONTH) - 1); calendar.add(Calendar.MONTH,-1); date = calendar.getTime();
java 由當前當前月得到上一個月
SimpleDateFormat sd=new SimpleDateFormat("yyyy-MM"); try { String payoffYearMonth = "2018-06"; Date currdate = sd.parse(payoffYearMonth); Calendar calendar= Calendar.getInstance(); calendar.setTime(currdate); calendar.set(Calendar.MONTH,calendar.get(Calendar.MONTH)-1); System.out.println(sd.format(calendar.getTime())); } catch (ParseException e) { e.printStackTrace(); }
到此這篇關于Java 獲取上一個月的月份的文章就介紹到這了,更多相關java獲取上一個月月份內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
SpringBoot配置Actuator組件,實現系統(tǒng)監(jiān)控
在生產環(huán)境中,需要實時或定期監(jiān)控服務的可用性。Spring Boot的actuator(健康監(jiān)控)功能提供了很多監(jiān)控所需的接口,可以對應用系統(tǒng)進行配置查看、相關功能統(tǒng)計等。2021-06-06老生常談Java中List與ArrayList的區(qū)別
大家都知道List是接口,ArrayList是List接口的一個實現類,接下來通過本文給大家介紹Java中List與ArrayList的區(qū)別,需要的朋友可以參考下2022-08-08SpringBoot+Shiro+LayUI權限管理系統(tǒng)項目源碼
本項目旨在打造一個基于RBAC架構模式的通用的、并不復雜但易用的權限管理系統(tǒng),通過SpringBoot+Shiro+LayUI權限管理系統(tǒng)項目可以更好的幫助我們掌握springboot知識點,感興趣的朋友一起看看吧2021-04-04