【问题标题】:finding all possible time combinations between a given time range查找给定时间范围内所有可能的时间组合
【发布时间】:2018-09-22 15:40:31
【问题描述】:

我需要在 12:09:47 到 14:20:55 的 2 个特定时间之间的所有可能的时间组合 (hh:mm:ss) 中找到唯一的数字。我目前正在做以下事情:

  1. 从开始时间获取秒值
  2. 将此秒的值加 1,然后生成新的 LocalTime 使用新的秒值。
  3. 比较这个新时间以确保它小于结束时间。
  4. 从 hh:mm:ss 获取数字并将它们添加到 Set 集合中 (Set<Integer> uniqueDigits = new TreeSet<Integer>();) 提供唯一编号

但是在添加 59 秒之后,我如何增加分钟和小时,直到时间范围结束?

到目前为止,我有以下代码:

public int timeCombinations (String startTime, String endTime) {


        LocalTime end = LocalTime.parse(endTime);
        int count = 0;
        for (LocalTime t = LocalTime.parse(startTime, DateTimeFormatter.ofPattern("HH:mm:ss")); t.isBefore(end); t=t.plusSeconds(1)) {
//          System.out.println(t);
//          String timeStr = t.toString();
//          int second = t.get(ChronoField.SECOND_OF_MINUTE);
            System.out.println(t);
            Set<Integer> uniqueDigits = new TreeSet<Integer>();
            String hour = String.valueOf(t.getHour());
            String mins = String.valueOf(t.getMinute());
            String secs = String.valueOf(t.getSecond());

            uniqueDigits.add(Integer.valueOf(String.valueOf(t.getHour()).substring(0,1)));
            if(hour.length()==2) {
                uniqueDigits.add(Integer.valueOf(String.valueOf(t.getHour()).substring(1)));
            }

            uniqueDigits.add(Integer.valueOf(String.valueOf(t.getMinute()).substring(0,1)));
            if(hour.length()==2) {
                uniqueDigits.add(Integer.valueOf(String.valueOf(t.getMinute()).substring(1)));
            }

            uniqueDigits.add(Integer.valueOf(String.valueOf(t.getSecond()).substring(0,1)));            
            if(secs.length()==2) {
                uniqueDigits.add(Integer.valueOf(String.valueOf(t.getSecond()).substring(0,1)));
            }

我确信有更好的方法可以达到同样的效果。有人可以建议另一种方式吗?

谢谢

【问题讨论】:

  • 如果您想比较时间,您应该使用内置的 Java API(例如LocalTime)而不是字符串。大部分工作已经为您完成
  • 如何获取开始和结束时间的时间戳并继续循环,直到您的开始时间戳达到结束时间戳,将当前时间戳格式化为您喜欢的字符串格式

标签: java sorting collections


【解决方案1】:

也许我错过了什么,但为什么不这样做:

static void timeCombinations(String startTime, String endTime) 
{
  DateTimeFormatter dtf = DateTimeFormatter.ofPattern("HH:mm:ss");
  LocalTime end = LocalTime.parse(endTime);
  for(LocalTime t=LocalTime.parse(startTime); t.isBefore(end); t=t.plusSeconds(1))
  {
    System.out.println(dtf.format(t));
  }
  System.out.println(end);
}

测试:

public static void main(String[] args)
{
  timeCombinations("12:09:59", "12:10:01");
}

输出:

12:09:59
12:10:00
12:10:01

【讨论】:

  • 感谢您的建议。它工作正常,但对于 12:12:00 的开始时间,我无法将秒数表示为 00。有没有办法可以用 2 位数字表示 0-9 之间的秒数?
  • 是的,您可以使用DateTimeFormatter。我已经编辑了答案以显示它的用途。它现在生成 12:12:00 而不是 12:12
  • 我已经试过了,但是没用,在我的问题中上传了新代码
  • 有什么建议,为什么这对我不起作用?
  • 什么不起作用?您问题中的代码看起来仍然太复杂。您可以在我的回答中使用该代码吗?还是我遗漏了您正在尝试做什么的一些细节?
【解决方案2】:

让我们先看看您采用的方法,即 String 方法。这绝对是可行的。它的代码如下。

public static void main(String [] args) {
    String time1 = "12:09:47";
    String time2 = "14:20:55";
    List<String> times = getAllTimeValues(time1, time2);
}

public static ArrayList<String> getAllTimeValues(String time1, String time2) {
    ArrayList<String> times = new ArrayList<String>();
    times.add(time1);

    while (!time1.equals(time2)) 
        times.add(time1 = addOneSecond(time1));

    return times;
}

public static String addOneSecond(String time) {
    String[] HoursMinutesSeconds = time.split(":");
    int hours = Integer.parseInt(HoursMinutesSeconds[0]);
    int minutes = Integer.parseInt(HoursMinutesSeconds[1]);
    int seconds = Integer.parseInt(HoursMinutesSeconds[2]);
    if (seconds == 59) {
        if (minutes == 59) {
            hours++;
            minutes = 0;
            seconds = 0;
        } else {
            minutes++;
            seconds = 0;
        }
    } else {
        seconds++;
    }
    return String.format("%02d", hours) + ":" + 
        String.format("%02d", minutes) + ":" + 
        String.format("%02d", seconds);

}

还不错,这种方法可以在很短的代码量内完成工作。大部分工作是使用 addOneSecond 方法完成的,该方法将检查秒是否为 60,然后检查分钟是否为 60。

但是,有一种更简单的方法。在 Java 8 中,它们更改了日期和时间,并且非常易于使用。让我们看看另一种方法来完成上述代码的作用。

public static void main(String [] args) {
    LocalTime time1 = LocalTime.of(12, 9, 47);
    LocalTime time2 = LocalTime.of(14, 20, 55);
    List<String> times = new ArrayList<String>();

    while (time1.isBefore(time2)) {
        times.add(time1.format(DateTimeFormatter.ISO_LOCAL_TIME).toString());
        time1 = time1.plusSeconds(1);
    }
}

这比第一个示例的代码要少得多!同样,在 Java 8 中,您可以使用 Date 和 Time 类的这些静态成员。它使随时检查变得容易得多。

【讨论】:

  • 使用String和DateTime比较的详细解释加分。
  • 另外,当它显示为 12:34 和 LocalTime .getSecond() on 12:34:01 只返回 1 而不是 01。您上面关于使用 format(DateTimeFormatter.ISO_LOCAL_TIME) 的建议效果很好!
【解决方案3】:

您可以使用LocalTime

LocalTime startLocalTime = LocalTime.parse(startTime);
LocalTime endLocalTime = LocalTime.parse(endTime);

while(startLocalTime.isBefore(endLocalTime)){
    startLocalTime = startLocalTime.plusSeconds(1);
    System.out.println(startLocalTime);
}

这将打印两个日期之间所有可能的日期。 更多信息Read Time Api

只是一个选项,可以使用 Duration 计算两次之间的可能时间。;

//duration calculates start and end date intervals.
Duration between = Duration.between(startLocalTime, endLocalTime);
//you can count how many seconds  or minutes or hours. 
//in your problem , getSeconds - 2 is your result between two time's possible
System.out.println(between.getSeconds());

在您的代码中,计算可能的日期计数。像这样使用负 2。因为在此计算中应删除开始日期和结束日期。

更多信息请阅读Duration,Period Usage

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多