使用java的Calendar對(duì)象獲得當(dāng)前日期
思路:
先獲得當(dāng)前季度的開始和結(jié)束日期,在當(dāng)前日期的基礎(chǔ)上往前推3個(gè)月即上個(gè)季度的開始和結(jié)束日期
/**
* @param flag true:開始日期;false:結(jié)束日期
* @return
*/
public static String getLastQuarterTime(boolean flag){
SimpleDateFormat shortSdf = new SimpleDateFormat("yyyy-MM-dd");
SimpleDateFormat longSdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String resultDate="";
Date now = null;
try {
Calendar calendar = Calendar.getInstance();
int currentMonth = calendar.get(Calendar.MONTH) + 1;
//true:開始日期;false:結(jié)束日期
if(flag){
if (currentMonth >= 1 && currentMonth <= 3)
calendar.set(Calendar.MONTH, 0);
else if (currentMonth >= 4 && currentMonth <= 6)
calendar.set(Calendar.MONTH, 3);
else if (currentMonth >= 7 && currentMonth <= 9)
calendar.set(Calendar.MONTH, 6);
else if (currentMonth >= 10 && currentMonth <= 12)
calendar.set(Calendar.MONTH, 9);
calendar.set(Calendar.DATE, 1);
now = longSdf.parse(shortSdf.format(calendar.getTime()) + " 00:00:00");
}else{
if (currentMonth >= 1 && currentMonth <= 3) {
calendar.set(Calendar.MONTH, 2);
calendar.set(Calendar.DATE, 31);
} else if (currentMonth >= 4 && currentMonth <= 6) {
calendar.set(Calendar.MONTH, 5);
calendar.set(Calendar.DATE, 30);
} else if (currentMonth >= 7 && currentMonth <= 9) {
calendar.set(Calendar.MONTH, 8);
calendar.set(Calendar.DATE, 30);
} else if (currentMonth >= 10 && currentMonth <= 12) {
calendar.set(Calendar.MONTH, 11);
calendar.set(Calendar.DATE, 31);
}
now = longSdf.parse(shortSdf.format(calendar.getTime()) + " 23:59:59");
}
calendar.setTime(now);// 設(shè)置日期
calendar.add(Calendar.MONTH, -3);
resultDate = longSdf.format(calendar.getTime());
} catch (Exception e) {
;
}
return resultDate;
}
相關(guān)文章
java如何通過FileOutputStream字節(jié)流向文件中寫數(shù)據(jù)
這篇文章主要介紹了java如何通過FileOutputStream字節(jié)流向文件中寫數(shù)據(jù)問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-12-12
華為鴻蒙系統(tǒng)應(yīng)用開發(fā)工具 DevEco Studio的安裝和使用圖文教程
HUAWEI DevEco Studio 是華為消費(fèi)者業(yè)務(wù)為開發(fā)者提供的集成開發(fā)環(huán)境(IDE),旨在幫助開發(fā)者快捷、方便、高效地使用華為EMUI開放能力。這篇文章主要介紹了華為鴻蒙系統(tǒng)應(yīng)用開發(fā)工具 DevEco Studio的安裝和使用圖文教程,需要的朋友可以參考下2021-04-04
Java構(gòu)造函數(shù)與普通函數(shù)用法詳解
本篇文章給大家詳細(xì)講述了Java構(gòu)造函數(shù)與普通函數(shù)用法以及相關(guān)知識(shí)點(diǎn),對(duì)此有興趣的朋友可以參考學(xué)習(xí)下。2018-03-03
Java Comparable 和 Comparator 的詳解及區(qū)別
這篇文章主要介紹了Java Comparable 和 Comparator 的詳解及區(qū)別的相關(guān)資料,Comparable 自然排序和Comparator 定制排序的實(shí)例,需要的朋友可以參考下2016-12-12
解決@springboottest注解無法加載src/main/resources目錄下文件
這篇文章主要介紹了解決@springboottest注解無法加載src/main/resources目錄下文件,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-01-01
Spring Boot數(shù)據(jù)庫鏈接池配置方法
這篇文章主要介紹了Spring Boot數(shù)據(jù)庫鏈接池配置方法,需要的朋友可以參考下2017-04-04
java父子節(jié)點(diǎn)parentid樹形結(jié)構(gòu)數(shù)據(jù)的規(guī)整
這篇文章主要介紹了java父子節(jié)點(diǎn)parentid樹形結(jié)構(gòu)數(shù)據(jù)的規(guī)整,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-07-07
java8新特性-lambda表達(dá)式入門學(xué)習(xí)心得
這篇文章主要介紹了java8新特性-lambda表達(dá)式入門學(xué)習(xí)心得,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-03-03

