燕鹏
3 years ago
1 changed files with 78 additions and 0 deletions
@ -0,0 +1,78 @@ |
|||||
|
import java.text.ParseException; |
||||
|
import java.text.SimpleDateFormat; |
||||
|
import java.util.*; |
||||
|
|
||||
|
/** |
||||
|
* @author yanpeng |
||||
|
* @version 1.0 |
||||
|
* @desc TODO |
||||
|
* @company 北京中经网软件有限公司 |
||||
|
* @date 2021/8/10 9:51 |
||||
|
*/ |
||||
|
public class DateUtil { |
||||
|
public static void main(String[] args) { |
||||
|
Map<String, Integer> beforeDate = null; |
||||
|
try { |
||||
|
beforeDate = getBeforeDate(0); |
||||
|
} catch (ParseException e) { |
||||
|
e.printStackTrace(); |
||||
|
} |
||||
|
System.out.println(beforeDate); |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* @param checkCalculateCycle 考核计算周期 0=日 1=周 2=月 3=季 4=年 |
||||
|
* @return |
||||
|
*/ |
||||
|
public static Map<String, Integer> getBeforeDate(int checkCalculateCycle) throws ParseException { |
||||
|
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); |
||||
|
Date parse = sdf.parse("2021-08-01"); |
||||
|
Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone("GMT+08:00")); |
||||
|
// calendar.setTime(parse);
|
||||
|
int year = calendar.get(Calendar.YEAR); |
||||
|
int month = calendar.get(Calendar.MONTH) + 1; //获取月份,0表示1月份
|
||||
|
int quarter = month / 3; |
||||
|
Map<String, Integer> map = new HashMap<String, Integer>(); |
||||
|
map.put("year", year); |
||||
|
if (checkCalculateCycle == 4) { |
||||
|
map.put("year", --year); |
||||
|
return map; |
||||
|
} else if (checkCalculateCycle == 3) { |
||||
|
if (month <= 3) { |
||||
|
map.put("year", --year); |
||||
|
map.put("quarter", 4); |
||||
|
} else { |
||||
|
if (month % 3 == 0) { |
||||
|
quarter--; |
||||
|
} |
||||
|
map.put("quarter", quarter); |
||||
|
} |
||||
|
return map; |
||||
|
} else if (checkCalculateCycle == 2) { |
||||
|
calendar.add(Calendar.MONTH, -1); |
||||
|
month = calendar.get(Calendar.MONTH) + 1; |
||||
|
map.put("year", calendar.get(Calendar.YEAR)); |
||||
|
map.put("month", month); |
||||
|
map.put("quarter", month % 3 == 0 ? (month / 3) : (month / 3 + 1)); |
||||
|
return map; |
||||
|
} else if (checkCalculateCycle == 1) { |
||||
|
calendar.add(Calendar.WEEK_OF_MONTH, -1); |
||||
|
month = calendar.get(Calendar.MONTH) + 1; |
||||
|
map.put("year", calendar.get(Calendar.YEAR)); |
||||
|
map.put("quarter", month % 3 == 0 ? (month / 3) : (month / 3 + 1)); |
||||
|
map.put("month", month); |
||||
|
map.put("week", calendar.get(Calendar.WEEK_OF_MONTH)); |
||||
|
return map; |
||||
|
} else if (checkCalculateCycle == 0) { |
||||
|
calendar.add(Calendar.DAY_OF_MONTH, -1); |
||||
|
month = calendar.get(Calendar.MONTH) + 1; |
||||
|
map.put("year", calendar.get(Calendar.YEAR)); |
||||
|
map.put("quarter", month % 3 == 0 ? (month / 3) : (month / 3 + 1)); |
||||
|
map.put("month", month); |
||||
|
map.put("week", calendar.get(Calendar.WEEK_OF_MONTH)); |
||||
|
map.put("day", calendar.get(Calendar.DAY_OF_MONTH)); |
||||
|
return map; |
||||
|
} |
||||
|
return map; |
||||
|
} |
||||
|
} |
Loading…
Reference in new issue