封装第三方接口对外提供服务 时间的处理

封装时间检查的接口

timestampValidDate(timestamp);

public  Integer timestampValidDate (Long timestamp){

String timestampTmp = timestamp.toString();
//timestamp精确到秒,长度是10位
if(timestampTmp.length() != 10){
logger.error("ERROR->CommonServiceImpl-->timestampValidDate|ERROR = timestamp length error|param:timestamp = "+timestamp);
return ConstantConfig.TIMESTAMP_VALIDDATE_FORMAT_ERROR;
}
Long nowTimestamp = System.currentTimeMillis()/1000;
Long startTimestamp = nowTimestamp - 300;
Long endTimestamp = nowTimestamp + 300;
if(startTimestamp == null || endTimestamp == null){
logger.error("ERROR->CommonServiceImpl-->timestampValidDate|ERROR = startTimestamp or endTimestamp is null|"
        +"param:"+"startTimestamp="+startTimestamp+"|endTimestamp="+endTimestamp);
return ConstantConfig.TIMESTAMP_VALIDDATE_SYSTEM_ERROR;
}

if(timestamp > startTimestamp &&  timestamp < endTimestamp){
return ConstantConfig.TIMESTAMP_VALIDDATE_SUCCESS;
}
return  ConstantConfig.TIMESTAMP_VALIDDATE_TIME_OUT;

}





根据不同的返回码来处理



//timestamp验证
Integer timestampResult = commonService.timestampValidDate(timestamp);
if(timestampResult == ConstantConfig.TIMESTAMP_VALIDDATE_FORMAT_ERROR){
ResponseUtil.responseObject2App(response, map, ResponseUtil.STATUS_ERROR_PARAM, "timestamp format  error" ,callback);
return;
}

if(timestampResult == ConstantConfig.TIMESTAMP_VALIDDATE_TIME_OUT){
ResponseUtil.responseObject2App(response, map, ResponseUtil.STATUS_TIME_OUT, "Time must be within 5 minutes" ,callback);
return;
}
if(timestampResult == ConstantConfig.TIMESTAMP_VALIDDATE_SYSTEM_ERROR){
ResponseUtil.responseObject2App(response, map, ResponseUtil.STATUS_FAIL, "system error" ,callback);
return;
}

猜你喜欢

转载自blog.csdn.net/yz18931904/article/details/80697310
今日推荐