gpt4 book ai didi

java - 按时间对一周中的几天进行排序

转载 作者:行者123 更新时间:2023-11-30 05:51:21 26 4
gpt4 key购买 nike

我希望从周日 00:00 开始排序。我的问题是此代码的零从星期四 8:00 开始。我按照分钟数排序,但从周四 8:00 开始为零。

这是我到目前为止的代码。

String[] lines = S.split("\n");
System.out.println(Arrays.toString(lines));

int longest = 0;
try {
for (int i = 0; i < lines.length - 1; i++) {
SimpleDateFormat formatter = new SimpleDateFormat("EEE HH:mm-HH:mm", Locale.US);
long diffMinutes;
diffMinutes = TimeUnit.MILLISECONDS.toMinutes(
formatter.parse(lines[i + 1]).getTime() -
formatter.parse(lines[i]).getTime());
if (longest < diffMinutes) {
longest = (int) diffMinutes;
}
}
} catch (ParseException e) {
e.printStackTrace();
}
return longest;

该函数采用这样的字符串

"Mon 01:00-23:00\\nTue 01:00-23:00\\nWed 01:00-23:00\\nThu 01:00-23:00\\nFri 01:00-23:00\\nSat 01:00-23:00\\nSun 01:00-21:00"

程序对字符串进行切片并将其存储在数组行中,然后我尝试对其进行排序。

最佳答案

我认为这个问题可以通过应用更多的分割来解决,因为你的String表示周期的形式是from-to而不是绝对值例如1320(分钟)。我会使用 java.time,尤其是 LocalTimeDuration 来进行正确的计算和比较。

我的代码基本上使用辅助方法执行以下操作(请参阅代码注释):

  1. "\n" 分割输入
  2. 将第一步的每个结果按空格分开,以便将星期几与一天中的时间分开
  3. 将第二次分割的第二个结果除以“-”以获得一天中的时间
  4. 将结果转换为适当的对象
  5. 将它们存储在适当的数据结构中
  6. 查找最大持续时间

这是我想到的:

import java.time.DayOfWeek;
import java.time.Duration;
import java.time.LocalTime;
import java.time.format.TextStyle;
import java.util.Collections;
import java.util.Locale;
import java.util.Map;
import java.util.Map.Entry;
import java.util.TreeMap;

class WeekdayDurationHandler {

public static void main(String[] args) {
// input String splitting...
String input = "Mon 01:00-23:00\nTue 01:00-23:00\nWed 01:00-23:00\nThu 01:00-23:00\nFri 01:00-23:00\nSat 01:00-23:00\nSun 01:00-21:00";
String[] times = input.split("\n");

// data structure for holding the durations per day
Map<DayOfWeek, Duration> weekdayDurations = new TreeMap<>();

// the result of the first splitting is unparseable, that's why some more
// splitting is applied
for (String s : times) {
// separate the day of week from the time duration
String[] sp = s.split(" ");
// split the duration into "from" and "to" (time of day)
String[] tp = sp[1].split("-");
// parse the day of week into an appropriate object
DayOfWeek dayOfWeek = parseDayOfWeek(sp[0]);
// parse the times of day into appropriate objects
LocalTime localTimeFrom = LocalTime.parse(tp[0]);
LocalTime localTimeTo = LocalTime.parse(tp[1]);
// calculate the duration between "from" and "to" time of day
Duration duration = Duration.between(localTimeFrom, localTimeTo);
// store them in the data structure
weekdayDurations.put(dayOfWeek, duration);
}

// print them
weekdayDurations.forEach((DayOfWeek dayOfWeek, Duration duration) -> {
System.out.println(dayOfWeek.getDisplayName(TextStyle.FULL_STANDALONE, Locale.getDefault()) + ": "
+ duration.toHours() + " hours (" + duration.toMinutes() + " minutes)");
});

System.out.println("######################################################");

// then print the maximum durations found
findMaxDurationsFrom(weekdayDurations).forEach((DayOfWeek dayOfWeek, Duration duration) -> {
System.out.println(dayOfWeek.getDisplayName(TextStyle.FULL_STANDALONE, Locale.getDefault()) + ": "
+ duration.toHours() + " hours (" + duration.toMinutes() + " minutes)");
});
}

private static DayOfWeek parseDayOfWeek(String weekday) {
switch (weekday.toLowerCase()) {
case "mon":
return DayOfWeek.MONDAY;
case "tue":
return DayOfWeek.TUESDAY;
case "wed":
return DayOfWeek.WEDNESDAY;
case "thu":
return DayOfWeek.THURSDAY;
case "fri":
return DayOfWeek.FRIDAY;
case "sat":
return DayOfWeek.SATURDAY;
case "sun":
return DayOfWeek.SUNDAY;
default:
throw new RuntimeException("Unparsable weekday: \"" + weekday + "\"");
}
}

private static Map<DayOfWeek, Duration> findMaxDurationsFrom(Map<DayOfWeek, Duration> weekDurations) {
final Map<DayOfWeek, Duration> maxDurations = new TreeMap<>();
// find the maximum duration as a reference for all equal durations
Duration maxDuration = findMaxDuration(weekDurations);
// go through all durations and store those equal to maxDuration (no matter what day of week)
weekDurations.forEach((DayOfWeek dayOfWeek, Duration duration) -> {
if (duration.equals(maxDuration)) {
maxDurations.put(dayOfWeek, duration);
}
});

return maxDurations;
}

private static <K, V extends Comparable<V>> V findMaxDuration(Map<K, V> map) {
Entry<K, V> maxEntry = Collections.max(map.entrySet(),
(Entry<K, V> e1, Entry<K, V> e2) -> e1.getValue().compareTo(e2.getValue()));
return maxEntry.getValue();
}
}

希望对您有帮助...

关于java - 按时间对一周中的几天进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53868016/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com