这个和上一次上一篇提到的增加时间段只是多了一个循环增加而已。
- package com.zbp;
- import java.text.ParseException;
- import java.text.SimpleDateFormat;
- import java.util.Calendar;
- import java.util.Date;
- public class DateDifference {
- private static String beginDate;
- private static String endDate;
- public static void main(String[] args) throws ParseException {
- SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd 00:00:00");
- SimpleDateFormat sdf1 = new SimpleDateFormat("yyyyMMdd HH:mm:ss");
- Calendar cal = Calendar.getInstance();// 获得当前时间
- System.out.println(cal.getTime());
- cal.add(Calendar.DAY_OF_YEAR, -1);// 取得当前时间的前一天
- for (int i = 0; i < 48; i++) {
- String formatDate = sdf.format(cal.getTime());// 格式化想要的类型
- Date p = sdf.parse(formatDate);
- cal.setTime(p);// 将Calendar的值设置成得到的时间
- cal.add(Calendar.MINUTE, 30 * i);
- beginDate = sdf1.format(cal.getTime());
- cal.add(Calendar.MINUTE, 30);
- endDate = sdf1.format(cal.getTime());
- System.out.println(i + "." + beginDate);
- System.out.println(i + "." + endDate);
- }
- }
- }
转载于:https://blog.51cto.com/237451446/585308