我正在嘗試生成一個間隔為 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)
}
uj5u.com熱心網友回復:
如果您需要創建適合 72 小時時間跨度的“盡可能多的 15 分鐘時段”,從今天午夜開始......那么我將利用 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 等,您可能希望將其保留為 aLocalDateTime并利用日期比較來了解位置按照你的順序;我相信您可以弄清楚這部分,因為您可以很容易地使用 Java.Time api 執行日期算術運算。
請注意,這不是唯一的,甚至可能不是最好的方法,但它是一種方法。
另一種方法是保持增量并獲取 startTime.plus(increment)。總而言之,Java Time API 非常簡單。
更新
看來您需要輕推才能完成此操作,所以我在操場上花了 15 分鐘,想出了一個非常幼稚的解決方案。劇透警報,沒有魔法。
您可以在此處找到更新后的 Playground ,并使用它進行游戲。
我很肯定可能會有很多優化,甚至可能有更好的方法來實作這一點,這些方法更實用。希望評論是不言自明的。
呼叫它,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
}
uj5u.com熱心網友回復:
使用回圈并迭代直到 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("d", hours) ":" String.format("d", minutes))
}
}
uj5u.com熱心網友回復:
您的問題令人困惑,因為您似乎將時間與持續時間值混合在一起。我會按照你的字面意思接受它。
使用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,PT2H30M,PT2H45M,PT3H,PT3H15M,PT3H30M,PT3H45M,PT4H,PT4H15M,PT4H30M,PT4H45M,PT5H,PT5H15M,PT5H30M,PT5H45M,PT6H ,PT6H15M,PT6H30M,PT6H45M,PT7H,PT7H15M,PT7H30M,PT7H45M,PT8H,PT8H15M,PT8H30M,PT8H45M,PT9H,PT9H15M,PT9H30M,PT9H45M,PT10H,PT10H15M,PT10H30M,PT10H45M,PT11H,PT11H15M,PT11H30M,PT11H45M,PT12H,PT12H15M ,PT12H30M,PT12H45M,PT13H,PT13H15M,PT13H30M,PT13H45M,PT14H,PT14H15M,PT14H30M,PT14H45M,PT15H,PT15H15M,PT15H30M,PT15H45M,PT16H,PT16H15M,PT16H30M,PT16H45M,PT17H,PT17H15M,PT17H30M,PT17H45M,PT18H,PT18H15M,PT18H30M ,PT18H45M,PT19H,PT19H15M,PT19H30M,PT19H45M,PT20H,PT20H15M,PT20H30M,PT20H45M,PT21H,PT21H15M,PT21H30M,PT21H45M,PT22H,PT22H15M,PT22H30M,PT22H45M,PT23H,PT23H15M,PT23H30M,PT23H45M,PT24H,PT24H15M,PT24H30M,PT24H45M,PT25H,PT25H15M,PT25H30M,PT25H45M,PT26H,PT26H15M,PT26H30M,PT26H45M,PT27H,PT27H15M,PT27H30M,PT27H45M,PT28H,PT28H15M,PT28H30M,PT28H45M,PT29H,PT29H15M,PT29H30M,PT29H45M,PT30H,PT30H15M, PT30H30M,PT30H45M,PT31H,PT31H15M,PT31H30M,PT31H45M,PT32H,PT32H15M,PT32H30M,PT32H45M,PT33H,PT33H15M,PT33H30M,PT33H45M,PT34H,PT34H15M,PT34H30M,PT34H45M,PT35H,PT35H15M,PT35H30M,PT35H45M,PT36H,PT36H15M,PT36H30M, PT36H45M,PT37H,PT37H15M,PT37H30M,PT37H45M,PT38H,PT38H15M,PT38H30M,PT38H45M,PT39H,PT39H15M,PT39H30M,PT39H45M,PT40H,PT40H15M,PT40H30M,PT40H45M,PT41H,PT41H15M,PT41H30M,PT41H45M,PT42H,PT42H15M,PT42H30M,PT42H45M, PT43H, PT43H15M, PT43H30M, PT43H45M, PT44H, PT44H15M, PT44H30M, PT44H45M, PT45H, PT45H15M, PT45H30M, PT45H45M, PT46H, PT46H15M, PT4456H30M, PTHPT47,6PT47H15M,PT47H30M,PT47H45M,PT48H,PT48H15M,PT48H30M,PT48H45M,PT49H,PT49H15M,PT49H30M,PT49H45M,PT50H,PT50H15M,PT50H30M,PT50H45M,PT51H,PT51H15M,PT51H30M,PT51H45M,PT52H,PT52H15M,PT52H30M,PT52H45M,PT53H,PT53H15M, PT53H30M,PT53H45M,PT54H,PT54H15M,PT54H30M,PT54H45M,PT55H,PT55H15M,PT55H30M,PT55H45M,PT56H,PT56H15M,PT56H30M,PT56H45M,PT57H,PT57H15M,PT57H30M,PT57H45M,PT58H,PT58H15M,PT58H30M,PT58H45M,PT59H,PT59H15M,PT59H30M, PT59H45M,PT60H,PT60H15M,PT60H30M,PT60H45M,PT61H,PT61H15M,PT61H30M,PT61H45M,PT62H,PT62H15M,PT62H30M,PT62H45M,PT63H,PT63H15M,PT63H30M,PT63H45M,PT64H,PT64H15M,PT64H30M,PT64H45M,PT65H,PT65H15M,PT65H30M,PT65H45M, PT66H, PT66H15M, PT66H30M, PT66H45M, PT67H, PT67H15M, PT67H30M, PT67H45M, PT68H, PT68H15M, PT68H30M, PT68H45M, PT69H, PT69H15M, PT465H30M, PTHPT6,9PT70H15M、PT70H30M、PT70H45M、PT71H、PT71H15M、PT71H30M、PT71H45M、PT72H]
I strongly suggest you do not present these values in clock time: 00:15 through 72:00. This format is inherently ambiguous and easily confused for being a time-of-day. Instead, use standard ISO 8601 format as seen above.
But if you insist, you can create such text.
for ( Duration d : durations )
{
String text = String.format( "d" , d.toHours() ).concat( ":" ).concat( String.format( "d" , d.toMinutesPart() ) );
System.out.println( text );
}
00:15
00:30
00:45
01:00
…
71:00
71:15
71:30
71:45
72:00
If what you really want is to to track every 15 minutes of 72 hours, knowing the date and time-of-day where each of those 15 minute increments lands. For this I would use a Map, a collection key-value pairings. In your case, the key would be a Duration object and the value would be a ZonedDateTime object to represent the date with time-of-day as seen in a particular time zone. Post another Question, if you want to pursue this route.
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/440839.html
