有些业务场景,会需要从一个时间段中取出每个几分钟的时间,我这是做了一个demo,为了便于学习,代码如下
public static void main(String[] args) throws ParseException {
String startTime = "2020-8-11 10:00";
String endTime = "2020-8-11 11:40";
timePeriodValue(startTime, endTime, 6);
}
public static void timePeriodValue(String startTime, String endTime, int space) throws ParseException {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
Long start = sdf.parse(startTime).getTime();
long end = sdf.parse(endTime).getTime();
while (start <= end) {
System.out.println("时间:" + sdf.format(start));
start = start + 60L * 1000 * space;
}
}
运行结果如下: