Java 獲取上一個(gè)月的月份的正確寫法
Java 獲取上一個(gè)月的月份的正確寫法
因最近在寫代碼的時(shí)候遇到了獲取上個(gè)月月份的問題yyyy-MM這個(gè)格式,根據(jù)給的工具類,獲取出來的值是有問題的,所以記錄以下。
問題方法
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM"); Date date = new Date(); System.out.println(nowSdf.format(date)); Calendar calendar = Calendar.getInstance(); // 設(shè)置為當(dāng)前時(shí)間 calendar.setTime(date); // 設(shè)置為上一個(gè)月 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(); // 設(shè)置為當(dāng)前時(shí)間 calendar.setTime(date); // 設(shè)置為上一個(gè)月 //calendar.set(Calendar.MONTH, calendar.get(Calendar.MONTH) - 1); calendar.add(Calendar.MONTH,-1); date = calendar.getTime();
java 由當(dāng)前當(dāng)前月得到上一個(gè)月
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(); }
到此這篇關(guān)于Java 獲取上一個(gè)月的月份的文章就介紹到這了,更多相關(guān)java獲取上一個(gè)月月份內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
SpringBoot配置Actuator組件,實(shí)現(xiàn)系統(tǒng)監(jiān)控
在生產(chǎn)環(huán)境中,需要實(shí)時(shí)或定期監(jiān)控服務(wù)的可用性。Spring Boot的actuator(健康監(jiān)控)功能提供了很多監(jiān)控所需的接口,可以對應(yīng)用系統(tǒng)進(jìn)行配置查看、相關(guān)功能統(tǒng)計(jì)等。2021-06-06springboot整合日志處理Logback的實(shí)現(xiàn)示例
Logback是由log4j創(chuàng)始人設(shè)計(jì)的又一個(gè)開源日志組件,本文主要介紹了springboot整合日志處理Logback,文中通過示例代碼介紹的非常詳細(xì),需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2024-01-01老生常談Java中List與ArrayList的區(qū)別
大家都知道List是接口,ArrayList是List接口的一個(gè)實(shí)現(xiàn)類,接下來通過本文給大家介紹Java中List與ArrayList的區(qū)別,需要的朋友可以參考下2022-08-08SpringBoot+Shiro+LayUI權(quán)限管理系統(tǒng)項(xiàng)目源碼
本項(xiàng)目旨在打造一個(gè)基于RBAC架構(gòu)模式的通用的、并不復(fù)雜但易用的權(quán)限管理系統(tǒng),通過SpringBoot+Shiro+LayUI權(quán)限管理系統(tǒng)項(xiàng)目可以更好的幫助我們掌握springboot知識點(diǎn),感興趣的朋友一起看看吧2021-04-04Java?LocalTime的常用時(shí)間操作總結(jié)
日常開發(fā)中,?我們會經(jīng)常遇到時(shí)間的運(yùn)算,?操作,?格式化等,?這篇文章主要為大家詳細(xì)介紹了LocalTime的常用時(shí)間操作,感興趣的小伙伴可以了解一下2023-11-11