我试图生成一个间隔15分钟的72小时时隙数组,但它从24小时后的00:00开始,所以基本上它打印整个24小时时隙3次,但我想从00:15..23:45..24:00..24:45..25:00..25:15..72:00
开始
我使用日历API、的经验较少
这是我的代码,
val start_duration = "00:00"
val end_duration = "72:00"
val _start_duration = durationFormatter.parse(start_duration)
val calenderStartDuration = Calendar.getInstance()
val calenderEndDuration = Calendar.getInstance()
calenderStartDuration.time = _start_duration
calenderEndDuration.time = calenderStartDuration.time.addHours(72)
while (calenderStartDuration.before(calenderEndDuration)){
calenderStartDuration.add(Calendar.MINUTE, 15)
val timeStr = durationFormatter.format(calenderStartDuration.time).toLowerCase()
listBookingDuration.add(timeStr)
}
如果您需要创建;多达15分钟的时段";这可以在72小时内完成,从今天午夜开始。。。然后我将利用java.timeapis:的强大功能
这是天真的伪代码,但你会得到这样的想法:
import java.time.*
fun main() {
val startTime = LocalTime.of(0, 0)
val today = LocalDate.now()
var current = LocalDateTime.of(today, startTime) //use datetime to account for day changes.
val endDateTime = current.plusHours(72)
val timeSlots = mutableListOf<LocalTime>()// or LocalDateTime if you need the "date" component as well.
timeSlots.add(current.toLocalTime()) // add the 1st interval
while (current.isBefore(endDateTime)) {
val newCurrent = current.plusMinutes(15)
timeSlots.add(newCurrent.toLocalTime())
current = newCurrent
}
println(timeSlots)
}
此打印:
[00:00, 00:15, 00:30, 00:45, 01:00, 01:15, 01:30, 01:45, 02:00, 02:15, 02:30, 02:45, 03:00, 03:15, 03:30, 03:45, 04:00, 04:15, 04:30, 04:45, 05:00, 05:15, 05:30, 05:45, 06:00, 06:15, 06:30, 06:45, 07:00, 07:15, 07:30, 07:45, 08:00, 08:15, 08:30, 08:45, 09:00, 09:15, 09:30, 09:45, 10:00, 10:15, 10:30, 10:45, 11:00, 11:15, 11:30, 11:45, 12:00, 12:15, 12:30, 12:45, 13:00, 13:15, 13:30, 13:45, 14:00, 14:15, 14:30, 14:45, 15:00, 15:15, 15:30, 15:45, 16:00, 16:15, 16:30, 16:45, 17:00, 17:15, 17:30, 17:45, 18:00, 18:15, 18:30, 18:45, 19:00, 19:15, 19:30, 19:45, 20:00, 20:15, 20:30, 20:45, 21:00, 21:15, 21:30, 21:45, 22:00, 22:15, 22:30, 22:45, 23:00, 23:15, 23:30, 23:45, 00:00, 00:15, 00:30, 00:45, 01:00, 01:15, 01:30, 01:45, 02:00, 02:15, 02:30, 02:45, 03:00, 03:15, 03:30, 03:45, 04:00, 04:15, 04:30, 04:45, 05:00, 05:15, 05:30, 05:45, 06:00, 06:15, 06:30, 06:45, 07:00, 07:15, 07:30, 07:45, 08:00, 08:15, 08:30, 08:45, 09:00, 09:15, 09:30, 09:45, 10:00, 10:15, 10:30, 10:45, 11:00, 11:15, 11:30, 11:45, 12:00, 12:15, 12:30, 12:45, 13:00, 13:15, 13:30, 13:45, 14:00, 14:15, 14:30, 14:45, 15:00, 15:15, 15:30, 15:45, 16:00, 16:15, 16:30, 16:45, 17:00, 17:15, 17:30, 17:45, 18:00, 18:15, 18:30, 18:45, 19:00, 19:15, 19:30, 19:45, 20:00, 20:15, 20:30, 20:45, 21:00, 21:15, 21:30, 21:45, 22:00, 22:15, 22:30, 22:45, 23:00, 23:15, 23:30, 23:45, 00:00, 00:15, 00:30, 00:45, 01:00, 01:15, 01:30, 01:45, 02:00, 02:15, 02:30, 02:45, 03:00, 03:15, 03:30, 03:45, 04:00, 04:15, 04:30, 04:45, 05:00, 05:15, 05:30, 05:45, 06:00, 06:15, 06:30, 06:45, 07:00, 07:15, 07:30, 07:45, 08:00, 08:15, 08:30, 08:45, 09:00, 09:15, 09:30, 09:45, 10:00, 10:15, 10:30, 10:45, 11:00, 11:15, 11:30, 11:45, 12:00, 12:15, 12:30, 12:45, 13:00, 13:15, 13:30, 13:45, 14:00, 14:15, 14:30, 14:45, 15:00, 15:15, 15:30, 15:45, 16:00, 16:15, 16:30, 16:45, 17:00, 17:15, 17:30, 17:45, 18:00, 18:15, 18:30, 18:45, 19:00, 19:15, 19:30, 19:45, 20:00, 20:15, 20:30, 20:45, 21:00, 21:15, 21:30, 21:45, 22:00, 22:15, 22:30, 22:45, 23:00, 23:15, 23:30, 23:45, 00:00]
你可以试着在Kotlin游乐场玩这个。
现在,如果你想要";第2天";要打印24:00、24:15、24:30、24:45、25:00等,您可能希望将其保留为LocalDateTime
,并利用当天的比较来了解您在序列中的位置;我相信您可以理解这一部分,因为您可以很容易地使用Java.Time api执行日期算术运算。
请注意,这不是唯一的,甚至可能不是最好的方法,但这是的方法。
另一种选择是保持增量并获取startTime.plus(increment(。总而言之,Java Time API非常简单。
更新
看来你需要推动才能完成这项工作,所以我在操场上花了15分钟,想出了一个非常天真的解决方案。剧透提醒,没有魔法。
你可以在这里找到更新后的操场,并使用它。
我确信可能会有很多优化,甚至可能有更好的方法来实现这一点,这些方法更具功能性。希望这些评论是不言自明的。
用transform(list)
调用它,就会得到一个打印为:的List<String>
[00:00, 00:15 ...omitted for brevity... 24:00, 24:15, 24:30, 24:45, 25:00 ...omitted for brevity... 70:30, 70:45, 71:00, 71:15, 71:30, 71:45, 72:00]
这是";变换";功能:
fun transform(source: MutableList<LocalDateTime>): List<String> {
val dayMultiplier = 24 //hours per day, duh!
val initialDay = source[0].toLocalDate()
val result = mutableListOf<String>()
source.forEach {
val time = it.toLocalTime()
val hour: Int = time.hour
// calculate the number of days between the dates
val daysBetween = ChronoUnit.DAYS.between(initialDay, it)
// Calculate the offset of hours based on the day difference
val hourOffset = daysBetween * dayMultiplier
//... and add them to the current hour.
val newHour = hour + hourOffset
// Add leading zeros (optional)
val reformattedHour = if (newHour < 10) "0$newHour" else newHour
val reformattedMinute = if (time.minute <10) "0${time.minute}" else time.minute
// Naively compose the new time
val newTime = "$reformattedHour:$reformattedMinute"
result.add(newTime)
}
return result
}
更新2
- 我认为Basil Bourque使用
Duration
更好(如这里的答案所示( - 这整件事可以(也可能应该(在同一个循环块中完成。由于您自然地在那里创建对象并将其放入列表中,因此没有什么可以阻止您添加额外的步骤,即添加乘数并直接存储字符串。如果时间线不与日期/时间绑定,而是与时间"绑定";"间隔";(又名:持续时间(
您保存的数据越完整,就越容易"变换它";投入到你需要的任何东西中。例如,如果你从ZonedDateTime中存储的只是分钟数,那么你将无法从中获取日期。。。但如果你保存了整个ZonedDateTime,你几乎可以从中得到任何东西。
使用循环并迭代到72小时(4320分钟(,并在15分钟后编写逻辑以获取值
for (i in 0 until 4321) {
val remainder = i % 15
if(remainder==0){
val hours = i / 60; //since both are ints, you get an int
val minutes = i % 60;
listBookingDuration.add(String.format("%02d", hours)+":"+String.format("%02d", minutes))
}
}
您的问题令人困惑,因为您似乎将一天中的时间与持续时间值混合在一起。我会照你写的那样认真对待。
Duration
使用Duration
对象表示未附着到时间轴的时间跨度。如果不涉及时间线,请不要使用任何日期-时间类。
请注意,java.time类使用不可变对象。因此,当增加15分钟时,我们产生一个新的Duration
对象,而不是改变("突变"(原始对象。
List < Duration > durations = new ArrayList <>();
Duration increment = Duration.ofMinutes( 15 );
Duration limit = Duration.ofHours( 72 );
Duration duration = Duration.ofMinutes( 15 ); // First starting value.
while ( duration.compareTo( limit ) <= 0 )
{
durations.add( duration );
// Set up the next loop.
duration = duration.plus( increment );
}
System.out.println( "durations = " + durations );
持续时间=[PT15M、PT30M、PT45M、PT1H、PT1H15M、PT1H 30M、PT 1H45M、PT 2H、PT2H15M、…PT69H、PT69H15M、PT 69H30M、PT69H45M、PT70H、PT70H15M
我强烈建议您不要在时钟时间00:15到72:00中显示这些值。这种格式本质上是模糊的,很容易被混淆为一天中的某个时间。相反,请使用标准ISO 8601格式,如上所述。
但如果你坚持,你可以创建这样的文本。
for ( Duration d : durations )
{
String text = String.format( "%02d" , d.toHours() ).concat( ":" ).concat( String.format( "%02d" , d.toMinutesPart() ) );
System.out.println( text );
}
00:15
00:30
00:45
01:00
…
71:00
71:15
71:30
71:45
72:00
如果你真正想要的是每15分钟跟踪72小时,知道每15分钟增量的日期和时间。为此,我将使用Map
,一个集合键值配对。在您的情况下,键将是一个Duration
对象,值将是ZonedDateTime
对象,以表示特定时区中的日期和时间。如果你想走这条路,再提出一个问题。