Java 时间,时间戳 往前/后叠加一个小时
public Map<Long, Map<String, Object>> statusDeviceStatusByDate(String strDate) {
String nowTime1 = sdf1.format(new Date());
Long nowTime = Long.valueOf(dateTimeStamp(nowTime1, "yyyy-MM-dd HH:mm:ss"));
Long startTime = Long.valueOf(dateTimeStamp(strDate, "yyyy-MM-dd HH:mm:ss"));
// 原时间戳+3600。
Long beforTime = startTime;
Long afterTime = beforTime + 3600;//(精确到秒)
Long afterTime2 = beforTime + 3600*1000;//(精确到毫秒)
//这个不太好用,直接用上面的时间戳累加
Date afterHourTime = dateRoll(new Date(beforTime),Calendar.HOUR,-1);
Long afterTime = Long.valueOf(dateTimeStamp(sdf2.format(afterHourTime),"yyyy-MM-dd HH:mm:ss"));
}
/**
* 实现当前时间往前推N小时
*
* @param date 精确到秒的字符串 new Date()
* @param i Calendar.HOUR
* @param d 1
*/
public Date dateRoll(Date date, int i, int d) {
// 获取Calendar对象并以传进来的时间为准
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
// 将现在的时间滚动固定时长,转换为Date类型赋值
calendar.add(i, d);
// 转换为Date类型再赋值
date = calendar.getTime();
return date;
}