美文网首页
时间统计队列不存在时补0

时间统计队列不存在时补0

作者: 周六不算加班 | 来源:发表于2020-04-29 16:51 被阅读0次

1、获取时间差方法

 /**
 * 计算两个时间点天数
 * @Author caody
 * @Date 2020/4/29 9:10
 * @Param
 * @return {@link }
 **/
public static Long getDifferDay(Date startTime,Date endTime){
    SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
    long l = endTime.getTime() - startTime.getTime();
    long day = l/(24*60*60*1000);
    return day+1;
}

2、存储对象

public class AltcPsTrackCountVO implements Serializable{

private static final long serialVersionUID = 1L;

@ApiModelProperty(value = "社区id",name="communityId")
private Long communityId;


@ApiModelProperty(value = "社区名字",name="communityName")
private String communityName;

@ApiModelProperty(value = "时间",name="datetime")
private String datetime;

@ApiModelProperty(value = "数量",name="number")
private Integer number;


public Long getCommunityId(){
    return communityId;
}

public void setCommunityId(Long communityId){
    this.communityId = communityId;
}


public String getDatetime(){
    return datetime;
}

public void setDatetime(String datetime){
    this.datetime = datetime;
}

public String getCommunityName() {
    return communityName;
}

public void setCommunityName(String communityName) {
    this.communityName = communityName;
}

public Integer getNumber() {
    return number;
}

public void setNumber(Integer number) {
    this.number = number;
}
}

3、主要方法

 public static List<AltcPsTrackCountVO> addDayTrackForNull(List<AltcPsTrackCountVO> oldList, Date startTime, long communityId){
    SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
    Date endTime = new Date();
    long n = LatestDateUtil.getDifferDay(startTime,endTime);
    ArrayList<AltcPsTrackCountVO> newList = new ArrayList<>();
    int num = oldList.size();
    int temp = 0;
    try {
        for (int i=0;i<n;i++){

            if (temp < num){
                if (startTime.compareTo(df.parse(oldList.get(temp).getDatetime())) < 0){
                    AltcPsTrackCountVO altcPsTrackCountVO = getNewAltcPsTrackCountVO(df.format(startTime),communityId);
                    newList.add(altcPsTrackCountVO);
                }else {
                    newList.add(oldList.get(temp));
                    temp ++;
                }
            } else if (temp >= num && startTime.compareTo(endTime) <= 0){
                AltcPsTrackCountVO altcPsTrackCountVO = getNewAltcPsTrackCountVO(df.format(startTime),communityId);
                newList.add(altcPsTrackCountVO);
            }
            startTime = LatestDateUtil.addDateOneDay(startTime);

        }
    } catch (ParseException e) {
        e.printStackTrace();
    }
    return newList;
}

4、天数增加

   public static Date addDateOneDay(Date date) {
    if (null == date) {
        return date;
    }
    Calendar c = Calendar.getInstance();
    c.setTime(date);   //设置当前日期
    c.add(Calendar.DATE, 1); //日期加1天

    date = c.getTime();
    return date;
}

5、补0方法

public static AltcPsTrackCountVO getNewAltcPsTrackCountVO(String startTime,Long communityId){
    AltcPsTrackCountVO altcPsTrackCountVO = new AltcPsTrackCountVO();
    altcPsTrackCountVO.setCommunityId(communityId);
    altcPsTrackCountVO.setDatetime(startTime);
    altcPsTrackCountVO.setNumber(0);
    return altcPsTrackCountVO;
}

相关文章

网友评论

      本文标题:时间统计队列不存在时补0

      本文链接:https://www.haomeiwen.com/subject/dmknwhtx.html