每间隔15分钟生成一个时间戳

时间:2022-01-07 02:25:57
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

public class Test {
public static void main(String[] args) {
Date date
= new Date();
List
<Date> ds = test(date);

SimpleDateFormat sdf
= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
for (Date d : ds) {
System.out.println(sdf.format(d));
}
}

static List<Date> test(Date date) {
Date start
= dayStartDate(date);//转换为天的起始date
Date nextDayDate = nextDay(start);//下一天的date

List
<Date> result = new ArrayList<Date>();
while (start.compareTo(nextDayDate) < 0) {
result.add(start);
//日期加15分钟
start = addFiveMin(start, 15);
}

return result;
}

private static Date addFiveMin(Date start, int offset) {
Calendar c
= Calendar.getInstance();
c.setTime(start);
c.add(Calendar.MINUTE, offset);
return c.getTime();
}

private static Date nextDay(Date start) {
Calendar c
= Calendar.getInstance();
c.setTime(start);
c.add(Calendar.DATE,
1);
return c.getTime();
}

private static Date dayStartDate(Date date) {
Calendar c
= Calendar.getInstance();
c.setTime(date);
c.set(Calendar.HOUR_OF_DAY,
0);
c.set(Calendar.MINUTE,
0);
c.set(Calendar.SECOND,
0);
c.set(Calendar.MILLISECOND,
0);
return c.getTime();
}
}