我试图生成一个间隔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)
}
发布于 2022-03-10 12:09:45
如果你需要创造“尽可能多的15分钟插槽”,可以在72小时的时间跨度内,从今天午夜开始.然后我将利用java.time apis的强大功能:
这是天真的伪代码,但您可以理解如下:
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操场上玩这个。
现在,如果您希望"day2“打印24:00、24:15、24:30、24:45、25:00等,您可能希望将其保存为一个LocalDateTime
,并利用当天的比较来了解您所在的序列中的位置;我相信您可以理解这一部分,因为您能够很容易地使用Java.Time apis执行日期算术操作。
注意,这不是唯一的,甚至不是最好的方法,但这是一种方法。
另一种选择是保留增量并获得startTime.plus(增量)。总之,Java非常简单。
更新
看来你需要一点推动才能完成这个任务,所以我花了15分钟在操场上,想出了一个非常天真的解决方案。剧透警报,没有魔法。
你可以找到在此更新操场,然后玩它。
我很肯定,有很多优化,甚至更好的方式来实现这一点,更多的功能。希望这些评论是不言自明的。
使用transform(list)
调用它,您将得到一个List<String>
,该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]
下面是"transform“函数:
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
Duration
更好(如这个答案在这里中所示),由巴兹尔伯克提供。保存的数据越完整,就越容易将其“转换”为所需的任何数据。如果您从ZonedDateTime存储的全部是分钟数,例如,您将无法从该日期.但是如果你拯救了整个ZonedDateTime,你几乎可以从它得到任何东西。
发布于 2022-03-10 11:09:54
使用循环并迭代到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))
}
}
发布于 2022-03-11 06:03:58
你的问题令人困惑,因为你似乎把一天的时间和期限的值混为一谈.我会像你写的那样接受它。
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、PT1H30M、PT1H45M、PT2H、PT2H15M、…PT69H,PT69H15M,PT69H30M,PT69H45M,PT70H,PT70H15M,PT70H30M,PT70H45M,PT71H,PT71H15M,PT71H30M,PT71H45M,PT72H
我强烈建议您不要在时钟时间: 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
对象,用于表示特定时区中的日期和日期。发布另一个问题,如果你想继续这条路线的话。
https://stackoverflow.com/questions/71422123
复制相似问题