记录自己日常写的通用方法。
根据日期(Date)获取哪年的第几季度,如下面代码:
public String GetQuarterByDate(String date) throws ParseException{
if(date == ""|| "".equals(date)){
return "";
}
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date datePar = sdf.parse(date);
Calendar calendar = Calendar.getInstance();
calendar.setTime(datePar);
String year = String.valueOf(calendar.get(Calendar.YEAR));
int mouth = datePar.getMonth()+1;
if(mouth>=1 && mouth<=3){
return year + "年第一季度";
}else if(mouth>=4 && mouth<=6){
return year + "年第二季度";
}else if(mouth>=7 && mouth<=9){
return year + "年第三季度";
}else if(mouth>=10 && mouth<=12){
return year + "年第四季度";
}else{
return "";
}
}
可传入字符串行日期,例如:2019-07-31。
也可传入其他格式的日期,自己转换一下就OK。