简体   繁体   English

需要每隔X分钟找到几小时到几小时

[英]need to find every X minutes between to hours

I have two hours lets say 12:00 and 14:00 (the input will always be two hours of "whatever" day) second time will always be greater than first time. 我有两个小时的时间,可以说12:00和14:00(输入将始终是“任何”天的两个小时),第二次将始终大于第一次。 So the input is simple : two given times in a day. 所以输入很简单:一天两次。 I need to find every 30 minutes between those two times. 我需要在两次之间每隔30分钟查找一次。

I need the following output (the ellapsed time between each output will always be 30 minutes in my case: 我需要以下输出(在我的情况下,每次输出之间的经过时间始终为30分钟:

12:00 12:30 13:00 13:30 14:00 14:30 12:00 12:30 13:00 13:30 14:00 14:30

I am discovering jodatime but I am a bit confused with how the determine "startTime" and "end Time" 我正在发现jodatime,但对如何确定“ startTime”和“ end Time”感到有些困惑

It's pretty easy, actually: 实际上,这很简单:

DateTime current = start;
while(true){
    System.out.println(current);
    current=current.plusMinutes(30);
    if(current.isAfter(stop))break;
}

Responding to comment: 回应评论:

to parse a String, you need a DateTimeFormatter . 要解析一个字符串,您需要一个DateTimeFormatter Here's one of several ways to acquire one: 这是获取一种的几种方法之一:

private static final DateTimeFormatter DATE_TIME_FORMATTER = 
    DateTimeFormat.forPattern("HH:mm");

Now you can do: 现在您可以执行以下操作:

DateTime startDate = DATE_TIME_FORMATTER.parseDateTime(startString);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM