<small id='5Nq01'></small><noframes id='5Nq01'>

      <bdo id='5Nq01'></bdo><ul id='5Nq01'></ul>

        <legend id='5Nq01'><style id='5Nq01'><dir id='5Nq01'><q id='5Nq01'></q></dir></style></legend>

        <i id='5Nq01'><tr id='5Nq01'><dt id='5Nq01'><q id='5Nq01'><span id='5Nq01'><b id='5Nq01'><form id='5Nq01'><ins id='5Nq01'></ins><ul id='5Nq01'></ul><sub id='5Nq01'></sub></form><legend id='5Nq01'></legend><bdo id='5Nq01'><pre id='5Nq01'><center id='5Nq01'></center></pre></bdo></b><th id='5Nq01'></th></span></q></dt></tr></i><div id='5Nq01'><tfoot id='5Nq01'></tfoot><dl id='5Nq01'><fieldset id='5Nq01'></fieldset></dl></div>
      1. <tfoot id='5Nq01'></tfoot>

        Java 格式小时和分钟

        时间:2023-09-29
      2. <small id='DiVVx'></small><noframes id='DiVVx'>

        • <bdo id='DiVVx'></bdo><ul id='DiVVx'></ul>
        • <legend id='DiVVx'><style id='DiVVx'><dir id='DiVVx'><q id='DiVVx'></q></dir></style></legend>

                <i id='DiVVx'><tr id='DiVVx'><dt id='DiVVx'><q id='DiVVx'><span id='DiVVx'><b id='DiVVx'><form id='DiVVx'><ins id='DiVVx'></ins><ul id='DiVVx'></ul><sub id='DiVVx'></sub></form><legend id='DiVVx'></legend><bdo id='DiVVx'><pre id='DiVVx'><center id='DiVVx'></center></pre></bdo></b><th id='DiVVx'></th></span></q></dt></tr></i><div id='DiVVx'><tfoot id='DiVVx'></tfoot><dl id='DiVVx'><fieldset id='DiVVx'></fieldset></dl></div>

                    <tbody id='DiVVx'></tbody>
                  <tfoot id='DiVVx'></tfoot>
                  本文介绍了Java 格式小时和分钟的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我需要像这样格式化我的时间字符串:

                  I need to format my time string such as this:

                  int time = 160;
                  

                  这是我的示例代码:

                  public static String formatDuration(String minute) {
                      String formattedMinute = null;
                      SimpleDateFormat sdf = new SimpleDateFormat("mm");
                      try {
                          Date dt = sdf.parse(minute);
                          sdf = new SimpleDateFormat("HH mm");
                          formattedMinute = sdf.format(dt);
                      } catch (ParseException e) {
                          e.printStackTrace();
                      }
                      return formattedMinute;
                  //        int minutes = 120;
                  //        int h = minutes / 60 + Integer.parseInt(minute);
                  //        int m = minutes % 60 + Integer.parseInt(minute);
                  //        return h + "hr " + m + "mins";
                  }
                  

                  我需要将其显示为 2 小时 40 分钟.但我不知道如何附加小时"和分钟".要求是不要使用任何库.

                  I need to display it as 2hrs 40mins. But I don't have a clue how to append the "hrs" and "mins". The requirement is not to use any library.

                  如果您过去做过类似的事情,请随时提供帮助.非常感谢!

                  If you've done something like this in the past, feel free to help out. Thanks a bunch!

                  推荐答案

                  既然是 2018 年,你真的应该使用 Java 8 中引入的日期/时间库

                  Since, it's 2018, you really should be making use of the Date/Time libraries introduced in Java 8

                  String minutes = "160";
                  Duration duration = Duration.ofMinutes(Long.parseLong(minutes));
                  
                  long hours = duration.toHours();
                  long mins = duration.minusHours(hours).toMinutes();
                  
                  // Or if you're lucky enough to be using Java 9+
                  //String formatted = String.format("%dhrs %02dmins", duration.toHours(), duration.toMinutesPart());
                  String formatted = String.format("%dhrs %02dmins", hours, mins);
                  System.out.println(formatted);
                  

                  哪些输出...

                  2hrs 40mins
                  

                  为什么要使用这样的东西?除了通常是更好的 API,当 minutes 等于 1600 时会发生什么?

                  Why use something like this? Apart of generally been a better API, what happens when minutes equals something like 1600?

                  上面将显示 26hrs 40mins,而不是打印 2hrs 40mins.SimpleDateFormat 格式化日期/时间值,它不处理持续时间

                  Instead of printing 2hrs 40mins, the above will display 26hrs 40mins. SimpleDateFormat formats date/time values, it doesn't deal with duration

                  这篇关于Java 格式小时和分钟的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:Java:如何以 ISO 8601 SECOND 格式获取当前日期 下一篇:如何在 jTextArea(或其他类型的控制台)中保留命令

                  相关文章

                  最新文章

                • <tfoot id='dnznU'></tfoot>
                  <i id='dnznU'><tr id='dnznU'><dt id='dnznU'><q id='dnznU'><span id='dnznU'><b id='dnznU'><form id='dnznU'><ins id='dnznU'></ins><ul id='dnznU'></ul><sub id='dnznU'></sub></form><legend id='dnznU'></legend><bdo id='dnznU'><pre id='dnznU'><center id='dnznU'></center></pre></bdo></b><th id='dnznU'></th></span></q></dt></tr></i><div id='dnznU'><tfoot id='dnznU'></tfoot><dl id='dnznU'><fieldset id='dnznU'></fieldset></dl></div>
                      <legend id='dnznU'><style id='dnznU'><dir id='dnznU'><q id='dnznU'></q></dir></style></legend>
                      • <bdo id='dnznU'></bdo><ul id='dnznU'></ul>

                      <small id='dnznU'></small><noframes id='dnznU'>