以半小時分割時間段(格式為08:00,09:00)的小方法
阿新 • • 發佈:2019-02-06
/**
* 半小時切割函式,如08:00到09:00 切割成 08:00、08:30、09:00
*/
public static void spiltTimeByHour(String startHour, String endHour) {
try {
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
long difTime = (sdf.parse(endHour).getTime() - sdf.parse(startHour)
.getTime()) / (1000);
int n = Integer.valueOf(String.valueOf((difTime / 60 / 30)));
Calendar c = Calendar.getInstance();
c.setTime(sdf.parse(startHour));
System.out.println(0+"->"+startHour);
for (int i = 1; i < n; i++) {
c.add(Calendar.MINUTE, 30);
System.out.println(i + "->" + sdf.format(c.getTime()));
}
System.out.println(n+"->"+endHour);
} catch (ParseException e) {
e.printStackTrace();
}
}