目前,我有一張包含以下條目的地圖:
Map<LocalDateTime, Integer> resultMap = new HashMap<LocalDateTime, Integer>();
//Put localdatetime and 30mins split indicator
for(Map.Entry<LocalDateTime, Integer> entry: resultMap.entrySet()) {
System.out.println(entry.getKey() " : " entry.getValue());
}
2022-03-29T00:30 : 1
2022-03-29T01:00 : 2
2022-03-29T01:30 : 3
2022-03-29T02:00 : 4
2022-03-29T02:30 : 5
2022-03-29T03:00 : 6
2022-03-29T03:30 : 7
.
.
.
2022-03-29T23:30 : 47
2022-03-30T00:00 : 48
我如何確定電流是否LocalDateTime在兩個時間之間?
例如,如果2022-03-29T01:27需要獲取當前時間的值3。
我能夠用iterator做到這一點。還有其他有效的方法嗎?
Iterator keyIterator = resultMap.entrySet().iterator();
Map.Entry<LocalDateTime, Integer> perviousEntry = new AbstractMap.SimpleEntry<LocalDateTime, Integer>(null, 0);
while(keyIterator.hasNext()) {
Map.Entry<LocalDateTime, Integer> NextEntry =
(Entry<LocalDateTime, Integer>) keyIterator.next();
if(null != perviousEntry.getKey()) {
if(LocalDateTime.now().isBefore(NextEntry.getKey()) && LocalDateTime.now().isAfter(perviousEntry.getKey())){
System.out.println(NextEntry.getKey() " Value : " NextEntry.getValue());
}
}
perviousEntry = NextEntry;
}
uj5u.com熱心網友回復:
您可以使用TreeMap.
要找出哪個dateTime物件是給定的最接近的物件,我們需要比較兩個鍵:
lo- 通過floorKey()(<=到給定的日期時間)獲得;hi- 使用ceilingKey()(>=到給定的dateTime)獲得。
為了比較lo和hi我們可以利用Duration類,它模擬了時間的數量。它的靜態方法between()允許獲取表示兩個時間Duration物件之間的間隔的物件。
以下是如何實作它的完整示例:
public class DataTimeStorage {
private final NavigableMap<LocalDateTime, Integer> storage = new TreeMap<>();
public int getClosest(LocalDateTime dateTime) {
LocalDateTime lo = storage.floorKey(dateTime);
LocalDateTime hi = storage.ceilingKey(dateTime);
if (lo == null && hi == null) throw new IllegalArgumentException();
if (lo == null) return storage.get(hi);
if (hi == null) return storage.get(lo);
return diffInMillis(lo, dateTime) < diffInMillis(dateTime, hi) ?
storage.get(lo) : storage.get(hi);
}
public long diffInMillis(LocalDateTime dateTime1, LocalDateTime dateTime2) {
return Duration.between(dateTime1, dateTime2).toMillis();
}
public void addItemsForDay(LocalDate day) {
int count = 0;
for (LocalDateTime i = day.atTime(0, 0);
i.toLocalDate().equals(day);
i = i.plusMinutes(30))
storage.put(i, count );
}
@Override
public String toString() {
return storage.toString();
}
}
main()- 演示
public static void main(String[] args) {
DataTimeStorage storage = new DataTimeStorage();
storage.addItemsForDay(LocalDate.of(2022,03,29));
System.out.println(storage.getClosest(
LocalDateTime.of(2022,03,29,01,27)));
}
輸出
3
uj5u.com熱心網友回復:
第一個想法:如果您知道地圖中的所有時間值都是 00 分鐘或 30 分鐘,您可以嘗試使用地圖中存在的 currentTimeRoundedToTheNext00 或 30min 值。
例如:(偽代碼,因為我不熟悉 LocalDateTime)
LocalDateTime currentTimeRoundedToTheNext00or30min = currentTime;
if (0 < currentTime.minutes < 30) {
currentTimeRoundedToTheNext00or30min.setMinutes(30);
} else {
currentTimeRoundedToTheNext00or30min.setMinutes(0);
currentTimeRoundedToTheNext00or30min.addHour(1);
}
int result = yourMap.get(currentTimeRoundedToTheNext00or30min);
思路二:如果對你的map中作為key的時間值進行排序,你的需求也可以看成是“找到map的第一個大于當前時間的key”。通過遍歷映射鍵,將鍵與 currentTime 進行比較,并在大于您測驗的 currentTime 值時立即退出回圈,您可以解決問題
第三個想法:即使不使用地圖,也有一種簡單的方法可以根據 currentTime 的小時和分鐘值找到期望值。(也是偽代碼,因為我不確定使用 LocalDateTime 函式的語法)
LocalDateTime currentTime = LocalDateTime.now();
Integer result = currentTime.getHours() * 2;
if (0 < currentTime.getMinutes() <= 30) {
result = 1;
} else {
result = 2;
}
這可以看作是“計算從一天開始以來已經開始的 30 分鐘部分的數量”
uj5u.com熱心網友回復:
要查找當前 LocalDateTime 是否在兩個時間之間,可以使用
LocalDateTime::isBefore和Duration::between。作為第一步,我們可以找出在當前時間之前發生的所有日期,在這些日期中,我們可以找出最近 15 秒(或任何其他有意義的時間段)內發生的日期。
類似的東西
resultMap.entrySet().stream()
.filter( entry -> {
val now = LocalDateTime.now();
return (entry.getKey() == now) || entry.getKey().isBefore(now) &&
Duration.between(entry.getKey(),now()).toSeconds() == 15)
)
.forEach( entry -> System.out.println(entry.getKey() " : " entry.getValue());
uj5u.com熱心網友回復:
你是正確的NavigableMap是一個很好的方法:
NavigableMap<LocalDateTime, Integer> sorted = new TreeMap<>(resultMap);
Map.Entry<LocalDateTime, Integer> next = sorted.ceilingEntry(targetTime);
OptionalInt match;
if (next != null) {
match = OptionalInt.of(next.getValue());
} else {
match = OptionalInt.empty();
}
我不確定這是否是您想要的確切邏輯,但您可以研究 , 和 的 javadocceilingEntry并選擇floorEntry適合您需要的那些。higherEntrylowerEntry
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/454585.html
