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

