123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261 |
- package com.huaxu.utils;
- import com.huaxu.evaluation.enums.EvaluationCycleEnums;
- import com.huaxu.evaluation.vo.EvaluationItemVo;
- import com.huaxu.util.DatesUtil;
- import org.apache.commons.lang3.StringUtils;
- import java.math.BigDecimal;
- import java.math.RoundingMode;
- import java.time.LocalDate;
- import java.time.temporal.TemporalAdjusters;
- import java.util.Calendar;
- import java.util.Date;
- import java.util.List;
- import java.util.stream.Collectors;
- /**
- * @ClassName EvaluationJobUtil
- * @Description: TODO
- * @Author lihui
- * @Date 2021/5/10
- * @Version V1.0
- **/
- public class EvaluationUtil {
- /**
- * 考评周期格式化
- */
- private static final String DATE_STRING = "%s年%s";
- private static Calendar getCalendar(){
- return Calendar.getInstance();
- }
- public static Calendar getCalendar (String dateTime){
- Calendar calendar = getCalendar();
- calendar.setTime(DatesUtil.parseDate(dateTime, "yyyy-MM-dd HH:mm:ss"));
- return calendar;
- }
- /***
- * 格式化考评周期时间
- * @author lihui
- * @date 11:00 2021/5/19
- * @param cycle :
- * @param year :
- * @param month :
- * @return java.lang.String
- **/
- public static String formatCycle(int cycle, String year, String month){
- String monthString = "";
- if (cycle != EvaluationCycleEnums.YEAR.getType()) {
- monthString = cycle == 0 ? month + "月" : month + "季度";
- }
- if ("3".equals(month)) {
- month = "1";
- } else if ("6".equals(month)){
- month = "2";
- } else if ("9".equals(month)){
- month = "3";
- } else if ("12".equals(month)){
- month = "4";
- }
- return String.format(DATE_STRING, year, monthString);
- }
- public static Integer toInteger(Long lon) {
- return lon == null ? null : lon.intValue();
- }
- public static BigDecimal divide(Integer completeCount, Integer total){
- String defaultValue = "0";
- BigDecimal bigDecimalComplete = new BigDecimal(completeCount.toString());
- BigDecimal bigDecimalTotal = new BigDecimal(total.toString());
- if (bigDecimalTotal.compareTo(new BigDecimal(defaultValue)) == 0){
- return new BigDecimal(defaultValue);
- }
- return bigDecimalComplete.divide(bigDecimalTotal,2, RoundingMode.HALF_UP);
- }
- /**
- * @Author lihui
- * @Description 转换分钟
- * @Date 17:11 2021/5/11
- * @Param [dataLimit]
- * @return int
- **/
- public static int minute(String dataLimit){
- if (StringUtils.isEmpty(dataLimit)){
- return 0;
- }
- return new BigDecimal(dataLimit).multiply(new BigDecimal("60")).intValue();
- }
- /**
- * @Author lihui
- * @Description 完成状态
- * @Date 17:08 2021/5/11
- * @Param [status]
- * @return boolean
- **/
- public static boolean completed(Integer status){
- return status == 2 || status == 3 ;
- }
- /**
- * @Author lihui
- * @Description 计算是否延期完成
- * @Date 11:06 2021/5/11
- * @Param [finishDate :最终完成时间, planFinishDate:预计完成时间, addMinute:可延期的时间]
- * @return boolean
- **/
- public static boolean isDelay(Date finishDate, Date planFinishDate, int addMinute){
- if (finishDate == null || planFinishDate == null) {
- return false;
- }
- return finishDate.after(DatesUtil.addMinuteOfDate(planFinishDate,addMinute));
- }
- public static Long[] toLong (List<Integer> userIds){
- Long [] longs = new Long[userIds.size()];
- for (int i = 0 ; i < userIds.size(); i++) {
- longs[i] =Long.parseLong(userIds.get(i).toString()) ;
- }
- return longs;
- }
- /**
- * @Author lihui
- * @Description 是否是当天
- * @Date 17:08 2021/5/11
- * @Param [evaluationDay]
- * @return boolean
- **/
- public static boolean isToday(int evaluationDay){
- return Calendar.getInstance().get(Calendar.DAY_OF_MONTH) == evaluationDay;
- }
- /**
- * @Author lihui
- * @Description 是否是同月
- * @Date 18:14 2021/5/11
- * @Param [month]
- * @return boolean
- **/
- public static boolean sameMonth(int month){
- return Calendar.getInstance().get(Calendar.MONTH) + 1 == month;
- }
- public static boolean containsType(List<EvaluationItemVo> itemEntityList, Integer type){
- List<String> list = itemEntityList.stream().map(EvaluationItemVo::getCycle).collect(Collectors.toList());
- for (String str: list) {
- if (str.indexOf(type.toString()) != -1) {
- return true;
- }
- }
- return false;
- }
- private static Integer calendarMonth(Integer calendarMonth){
- switch (calendarMonth){
- case 1 :
- return 4;
- case 4 :
- return 1;
- case 7 :
- return 2;
- case 10 :
- return 3;
- default: ;
- }
- return null;
- }
- private static String toString (Integer var){
- String result = var.toString();
- return result.length() == 1 ? "0" + result : result;
- }
- public static String getStartTime(int cycle, int day) {
- LocalDate localDate = LocalDate.now();
- if (cycle == EvaluationCycleEnums.MONTH.getType()) {
- localDate = localDate.plusMonths(-1);
- }
- if (cycle == EvaluationCycleEnums.QUARTER.getType()){
- Integer type = calendarMonth(localDate.getMonthValue());
- if (type == null) {
- return null;
- }
- int month = type == 1 ? 1 :
- type == 2 ? 4 :
- type == 3 ? 7 :
- type == 4 ? 10 : null;
- localDate = type == 4 ? localDate.plusYears(-1) : localDate;
- localDate = localDate.withMonth(month);
- }
- if (cycle == EvaluationCycleEnums.YEAR.getType()){
- localDate = localDate.plusYears(-1);
- localDate = localDate.withMonth(1);
- }
- localDate = localDate.withDayOfMonth(day);
- return localDate.toString() + " 00:00:00";
- }
- public static String getEndTime(int cycle, int day) {
- LocalDate localDate = LocalDate.now();
- if (cycle == EvaluationCycleEnums.MONTH.getType()) {
- localDate = day == 1 ? localDate.plusMonths(-1) : localDate;
- }
- if (cycle == EvaluationCycleEnums.QUARTER.getType()){
- Integer type = calendarMonth(localDate.getMonthValue());
- if (type == null) {
- return null;
- }
- int month = type == 1 ? 4 :
- type == 2 ? 7 :
- type == 3 ? 10 :
- type == 4 ? 1 : null;
- // 设置月份
- if (type == 4 && day == 1 ){
- localDate = localDate.plusMonths(-1);
- } else {
- localDate = localDate.withMonth(day == 1 ? month -1 : month);
- }
- }
- if (cycle == EvaluationCycleEnums.YEAR.getType()){
- localDate = localDate.withMonth(1);
- localDate = day == 1 ? localDate.plusMonths(-1) : localDate;
- }
- localDate = day != 1 ? localDate.withDayOfMonth(day - 1) : localDate.with(TemporalAdjusters.lastDayOfMonth());
- return localDate.toString() + " 23:59:59";
- }
- public static LocalDate getLocalDateByCycle(String startTime, String endTime, int cycle){
- // 季度获取后面的时间
- String quarterString = "4,7,10,1";
- boolean isQuarter = cycle == EvaluationCycleEnums.QUARTER.getType();
- String time = isQuarter ? endTime : startTime;
- LocalDate localDate = DatesUtil.parseLocalDate(time);
- if (isQuarter && quarterString.indexOf(localDate.getMonthValue() + "") != -1) {
- localDate = localDate.plusMonths(-1);
- }
- return localDate;
- }
- public static void main(String[] args) {
- LocalDate localDate = EvaluationUtil.getLocalDateByCycle("2020-12-01 00:00:00","2021-01-30 00:00:00",1);
- System.out.println(localDate.getYear() + "-" + localDate.getMonthValue());
- }
- }
|