java - 如何将军事时间整数转换为标准时间字符串

标签 java

所以我正在尝试编写一个程序来告诉用户他们可以乘坐什么巴士去上课。我现在有一个问题:我想让用户传递一个 int 来与 int 值的 ArrayList 进行比较,以告诉他们是否可以 catch 那辆巴士。

但是,我想将输出转换为 12 小时类型字符串,但我找不到有效的方法!这是到目前为止我的代码。

package bus;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.*;
public class busSchedule {
    public static void main(String[]args){
        int classTime = 0;
        Scanner scnr = new Scanner(System.in);
        System.out.print("What time do you have to be in class?: ");
        classTime = scnr.nextInt();

        for(int i = 0; i < AB.toCampus.size() - 1; ++i){
            if(classTime > AB.toCampus.get(i)){
                String timeString = Integer.toString(AB.toCampus.get(i));
                DateFormat f2 = new SimpleDateFormat("h:mm aa");
                String newTime = f2.format(timeString).toLowerCase();
                System.out.println("You can take the " + AB.line + " line at " + newTime + " to get to class.");
            }

        }
    }
}

我还有类AB,它是该行的名称。如下:

package bus;

import java.util.ArrayList;
import java.util.Arrays;

public class AB {
    static String line = "A-B";
    static ArrayList<Integer> toCampus = new ArrayList<Integer>(
            Arrays.asList(623, 1234, 1734, 2100)
            );

}

实际上,输出仅一遍又一遍地打印 4:00 pm。

感谢任何帮助!

最佳答案

您可以简单地将军事时间解析为时间类别,然后将其格式化为您想要的输出,例如......

ArrayList<Integer> toCampus = new ArrayList<>(
                Arrays.asList(623, 1234, 1734, 2100)
);

for (int time : toCampus) {
    String value = String.format("%04d", time);
    LocalTime lt = LocalTime.parse(value, DateTimeFormatter.ofPattern("HHmm"));
    System.out.println(DateTimeFormatter.ofPattern("hh:mm a").format(lt));
}

哪些输出

06:23 AM
12:34 PM
05:34 PM
09:00 PM

现在,由于您的数据实际上已排序,因此您可以使用 Collections.binarySearch 来查找匹配项,例如...

ArrayList<Integer> toCampus = new ArrayList<>(
                Arrays.asList(623, 1234, 1734, 2100)
);

Scanner scnr = new Scanner(System.in);
System.out.print("What time do you have to be in class?: ");
int classTime = scnr.nextInt();

int index = Collections.binarySearch(toCampus, classTime);
System.out.println(index);
if (index < 0) {
    index = Math.abs(index) - 1;
}

if (index > 0 && index <= toCampus.size()) {
    int time = toCampus.get(index - 1);
    String value = String.format("%04d", time);
    LocalTime lt = LocalTime.parse(value, DateTimeFormatter.ofPattern("HHmm"));
    System.out.println(DateTimeFormatter.ofPattern("hh:mm a").format(lt));
} else {
    System.out.println("Go back to bed");
}

Collections.binarySearch找不到匹配项时,它将返回“-(插入点)- 1”,这告诉我们项目会出现在哪里,如果它在列表中。

这使我们能够就列表中的哪个值最合适做出一些决定。

例如,如果我们输入630binarySearch将返回-2,添加1作为offset,它给我们 -1,将其转换为正数,结果就是 1。现在,显然 1 处的元素是 1234,但如果我们采用前一个元素,它将返回 623!

关于java - 如何将军事时间整数转换为标准时间字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33250090/

相关文章:

java - 在java中解析java字符串

java - 如何在 HTML 中嵌入主类不在 JAR 主目录中的 Java 应用程序?

java - 如何在 Java 中访问深层 JSON 元素

java - 更新 Jooq 的自动生成类

java - 在 Linux 上编写要由 Windows 应用程序导入的文本文件,换行问题

java - Consumer<T> 映射到 HashMap 中的 Class<T>

java - 在 Java 的二维字符数组中搜索 'bubbles'

Java Socket 编程 - HTTP 1.1 的 301 错误

java - 使用 JDBC 的 NullPointerError

java - 从字符串中删除不可打印字符的更好方法是什么