public class QuarterUtil {
/**
* 获取 x 个季度之前的日期
* @param x
* @return
*/
public static Date getBeforQuarterStartDate(Date nowDate,int x){
Calendar calendar = Calendar.getInstance();
calendar.setTime(nowDate);
int tempYear =calendar.get(Calendar.YEAR);
int m = calendar.get(Calendar.MONTH)+1;
int currentQuarter = getCurrentQuarter(m);
int tempQuarter = currentQuarter;
for(int i = 0 ;i < x ;i++){
if(i == 0){
tempQuarter = currentQuarter - 1;
}else{
tempQuarter = tempQuarter - 1;
}
//年度减一
if(tempQuarter <= 0){
tempYear-=1;
tempQuarter=4;
}
}
String dateTime = tempYear+""+getQuarterStartDate(tempQuarter);
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
try{
return sdf.parse(dateTime);
}catch(Exception e){
e.printStackTrace();
}
return null;
}
/**
* 获得当前季度
* @param m
* @return
*/
public static int getCurrentQuarter(int m){
if(m>= 1 && m <=3){
return 1;
}else if(m>= 4 && m <=6){
return 2;
}else if(m>= 7 && m <=9){
return 3;
}else if(m>= 10 && m <=12){
return 4;
}
return -1;
}
/**
* 获得季度的开始日期
* @param q
* @return
*/
public static String getQuarterStartDate(int q){
if(q == 1){
return "0101";
}else if(q == 2){
return "0401";
}else if(q == 3){
return "0701";
}else if(q == 4){
return "1001";
}
return "";
}
public static void main(String[] args) {
System.out.println(getBeforQuarterStartDate(new Date(),0));
}
Java获得前n个季度的起始日期
最新推荐文章于 2023-05-24 11:15:21 发布