格式化小时 java 20h 10m 5000s 到 20h 10m 10s

Formatting hour in java 20h 10m 5000s to 20h 10m 10s

我正在尝试创建一个小程序,我们给出了错误的时间,例如:20h 10m 5000s,然后将其转换为 20h 10m 50s。但是我无法给你看代码,看看你能不能帮我,非常感谢:)

import java.util.Date;
import java.text.SimpleDateFormat;
import javax.swing.JOptionPane;

public class EejercicioBasico3 {
    public static void main(String[] args) {
        
        Date date = new Date();
        SimpleDateFormat dateForm = new SimpleDateFormat("HH:mm:ss");
 
        String UserDate = dateForm.format(JOptionPane.showInputDialog("Escriba una hora en formato hh-mm-ss"));
        
        System.out.println(date);
        System.out.println(UserDate);
    
    }
}

删除多余的数字

根据您的问题和评论,我倾向于理解您假设用户可能会错误地键入太多数字。我进一步假设每个数字可能在从 0 或 00 到 59 的区间内,并且任何使数字大于 59 或宽于两位数的数字都将被删除。它可能并不完美,但可以帮助您入门。

    String inputTimeString = "20h 10m 5000s";
    String outputTimeString
            = inputTimeString.replaceAll("([6-9]|[0-5]\d)\d+", "");
    System.out.println(outputTimeString);

输出为:

20h 10m 50s

正则表达式首先匹配 6 – 9 范围内的一个数字或从 0 到 5 开头的两个数字,以确保我们最多得到 59。这个或这些数字被捕获为一个组,使用围绕在正则表达式中分组。在该组之后,匹配任意数量的多余数字。在替换字符串中,我使用 </code> 来表示应该将数字替换为捕获组号中匹配的数字。 1(本例中唯一的捕获组)。</p> <p>试试另一个例子:</p> <pre><code> String inputTimeString = "60h 010m 777s";

6h 01m 7s

预订:如果这是学校的基本练习,您的老师可能会想到其他解决方案,但您可以更好地判断。如果你还没有学过正则表达式,你可能不应该提交使用它们的解决方案。也许您需要遍历输入字符串并将可以接受的字符添加到您收集输出的字符串缓冲区。

将多余的秒数转换为分钟数和小时数

如果您想要将多余的秒数(超过 59 秒)转换为分钟和小时,请使用 Duration class:

    String isoTimeString = "PT" + inputTimeString.replaceAll(" ", "");
    Duration dur = Duration.parse(isoTimeString);
    String outputTimeString = String.format("%dh %dm %ds",
            dur.toHours(), dur.toMinutesPart(), dur.toSecondsPart());
    
    System.out.println(outputTimeString);

21h 33m 20s

Duration.parse() 需要 ISO 8601 格式。这是通过前缀 PT(认为 时间段 )并删除空格从您的格式中获得的。 String.format() 调用会重现您的格式。

始终避免 Date 和 SimpleDateFormat

您尝试使用的 classes,SimpleDateFormatDate,设计不佳且早已过时,根本不适合这样的工作。我建议您永远不要使用它们,并始终使用 java.time、现代 Java 日期和时间 API 作为您的时间工作。 Duration class 是 java.time.

的一部分

链接

在我看来底线是...只是不接受条目:20h 10m 5000s。虽然数据是通过 输入对话框 window 提供的,但仍然可以验证它是否包含所需的格式(很明显 在对话框中作为示例显示 ),如果不是,则通知用户再次输入。您的代码真的不需要适应每个拼写错误并自动更正它。然而,它应该确定存在拼写错误并通知用户更正它或完全丢弃输入数据。你的应用程序设置规则,而不是用户(不幸的是,这可能不是所有情况)。这可能看起来很直率,但是,你不能让所有的事情都证明是白痴,因为明天,只会有一个更好的白痴。让白痴做对。

确定您的应用程序的时间输入规则:

  • 时间以 三个 特定单位表示:小时分钟,以及 .
  • 时间采用 24 小时格式,这意味着没有上午或下午这样的时间。
  • 每个时间单位(小时、分钟或秒)由 两个 整数组成(例如:15-32-05)。
  • 分隔符必须用于每次分隔 单元。这种情况下允许的字符是连字符 (-) 或减号字符。

String userTime = "";
while (userTime.isEmpty()) {
    userTime = JOptionPane.showInputDialog(null, "<html>Enter a time in "
                + "<font color=red><b>hh-mm-ss</b></font> format:<br><br></html>");
    if (userTime == null) {
        JOptionPane.showMessageDialog(null, "Time entry Canceled!", "Entry Canceled",
                JOptionPane.WARNING_MESSAGE);
        return;
    }
    if (!userTime.matches(
               "^([0-1][0-9][-]|[2][0-3][-])([0-5][0-9][-])([0-5][0-9])$")) {
        JOptionPane.showMessageDialog(null, "Invalid Time format supplied!", 
                                "Invalid Entry", JOptionPane.WARNING_MESSAGE);
        userTime = "";
    }
}

String[] timeUnits = userTime.split("-");
String time = new StringBuilder("").append(timeUnits[0]).append("h ")
                 .append(timeUnits[1]).append("m ").append(timeUnits[2])
                 .append("s").toString();
JOptionPane.showMessageDialog(null, "<html>User supplied the time of:<br><br>"
        + "<center><font color=blue><b>" + time + "</b></font></center></html>",
        "Invalid Entry", JOptionPane.INFORMATION_MESSAGE);

显然你不需要在循环中做这种事情,但我敢肯定你明白了。