如何找出给定日期和开始和结束时间可用的所有持续时间:java

时间:2015-05-18 06:54:52

标签: java date

我的日期和时间格式为开始日期和时间15-05-15 02:00:AM 结束日期和时间15-05-15 06:00:AM. with the time interval of 60分钟`

如何找出这两个日期和时间之间的可用时间?

结果将是

start Time : 15-05-15 02:00:AM end Time : 15-05-15 03:00:AM
start Time : 15-05-15 03:00:AM end Time : 15-05-15 04:00:AM
start Time : 15-05-15 05:00:AM end Time : 15-05-15 06:00:AM

如果开始日期时间和结束日期时间范围在第二天范围内,请帮我处理。

我正在使用java 7

由于

2 个答案:

答案 0 :(得分:4)

Java 8

LocalDateTime#parse开始,将String值解析为LocalDateTime

然后使用循环,增加" start"的值。 LocalDateTime使用plusMinutes,直到价值为equalisAfter,然后"结束&#34} LocalDateTime

String startAt = "15-05-15 02:00:AM";
String endAt = "15-05-15 06:00:AM";

String format = "dd-MM-yy hh:mm':'a";

LocalDateTime startTime = LocalDateTime.parse(startAt, DateTimeFormatter.ofPattern(format));
LocalDateTime endTime = LocalDateTime.parse(endAt, DateTimeFormatter.ofPattern(format));
while (endTime.isAfter(startTime)) {
    LocalDateTime next = startTime.plusMinutes(60);
    System.out.println("StartTime : " + startTime.format(DateTimeFormatter.ofPattern(format)) + "; End Time : " + next.format(DateTimeFormatter.ofPattern(format)));
    startTime = next;
}

打印出来......

StartTime : 15-05-15 02:00:AM; End Time : 15-05-15 03:00:AM
StartTime : 15-05-15 03:00:AM; End Time : 15-05-15 04:00:AM
StartTime : 15-05-15 04:00:AM; End Time : 15-05-15 05:00:AM
StartTime : 15-05-15 05:00:AM; End Time : 15-05-15 06:00:AM

Joda-Time(其他所有人)

String startAt = "15-05-15 02:00:AM";
String endAt = "15-05-15 06:00:AM";

String format = "dd-MM-yy hh:mm':'a";
DateTimeFormatter formatter = DateTimeFormat.forPattern(format);

LocalDateTime startTime = formatter.parseLocalDateTime(startAt);
LocalDateTime endTime = formatter.parseLocalDateTime(endAt);

while (endTime.isAfter(startTime)) {
    LocalDateTime next = startTime.plusMinutes(60);
    System.out.println("StartTime : " + formatter.print(startTime) + "; End Time : " + formatter.print(next));
    startTime = next;
}

打印

StartTime : 15-05-15 02:00:AM; End Time : 15-05-15 03:00:AM
StartTime : 15-05-15 03:00:AM; End Time : 15-05-15 04:00:AM
StartTime : 15-05-15 04:00:AM; End Time : 15-05-15 05:00:AM
StartTime : 15-05-15 05:00:AM; End Time : 15-05-15 06:00:AM

"我不允许使用第三方库" (非推荐方法)

try {
    String startAt = "15-05-15 02:00:AM";
    String endAt = "15-05-15 06:00:AM";

    String format = "dd-MM-yy hh:mm':'a";
    SimpleDateFormat sdf = new SimpleDateFormat(format);
    Date startDate = sdf.parse(startAt);
    Date endDate = sdf.parse(endAt);

    Calendar startCal = Calendar.getInstance();
    startCal.setTime(startDate);
    Calendar endCal = Calendar.getInstance();
    endCal.setTime(endDate);

    while (endCal.after(startCal)) {

        String start = sdf.format(startCal.getTime());
        startCal.add(Calendar.MINUTE, 60);
        String to = sdf.format(startCal.getTime());

        System.out.println("Start Time : " + start + "; End Time : " + to);

    }

} catch (ParseException exp) {
    exp.printStackTrace();
}

哪个打印......

Start Time : 15-05-15 02:00:AM; End Time : 15-05-15 03:00:AM
Start Time : 15-05-15 03:00:AM; End Time : 15-05-15 04:00:AM
Start Time : 15-05-15 04:00:AM; End Time : 15-05-15 05:00:AM
Start Time : 15-05-15 05:00:AM; End Time : 15-05-15 06:00:AM

答案 1 :(得分:0)

java.util.Date startDate;
java.util.Date endDate;
java.util.Date currDate = startDate;

while (currDate < endDate) {
    Calendar cal = new GregorianCalendar();
    cal.setTime(currDate);
    cal.add(Calendar.HOUR_OF_DAY, 1);

    System.out.println("StartTime : " + startTime.format(DateTimeFormatter.ofPattern(format)) + "; End Time : " + cal.getTime().format(DateTimeFormatter.ofPattern(format)));
    currDate = cal.getTime();
}