数据同步时间处理,数据采集的时间,数据采集时间归属为有特征时间节点

时间:2022-11-27 18:34:24
以下代码以五分钟做测试
@Test
public void testAction(){
//当前时间比如为 2018-12-24 15:49:20
String datestr =getFiveSyncData("2018-12-24 15:49:20(时间类型哦,这里写字符串)",5)
system.out.println(datastr);
//打印输出 2018-12-24 15:45:00
String datestr1 =getFiveSyncData("2018-12-24 15:40:20(时间类型哦,这里写字符串)",5)
system.out.println(datastr1);
//打印输出 2018-12-24 15:45:00
}



/**
* 数据同步每五分钟进行数据归属统计
* @param time
* @return
*/
public static String getFiveSyncData(Date time,int belongTime){
Date castedTime =getTenMinutePoint(time,belongTime);
if(castedTime.getTime()>new Date().getTime()){
castedTime = new Date(castedTime.getTime()-1000*60*belongTime);
   }

String s =DD2S(castedTime);

return s;
}



/**
* 获得当前时间10分钟的整数时间点
* @return
*/
private static Date getTenMinutePoint(Date date,int belongTime){
int comareMinu=0;
Calendar rightNow = Calendar.getInstance();
rightNow.setTime(date);

int minute = rightNow.get(Calendar.MINUTE);
comareMinu=minute;
int sec = rightNow.get(Calendar.SECOND);
minute = Math.round(minute/belongTime*belongTime);//计算五分钟的整数分钟
if(comareMinu>minute || sec>0){
minute+=belongTime;
}


rightNow.set(Calendar.MINUTE, minute);
rightNow.set(Calendar.SECOND, 0);
//String time =rightNow.getTime()+"";
Date time =rightNow.getTime();
return time;
}