【问题标题】:converting seconds to hours,minutes, and seconds?将秒转换为小时、分钟和秒?
【发布时间】:2014-07-05 21:37:37
【问题描述】:

所以我试图制作一个可以将 s 从输入转换为 h、m 和 s 的程序。到目前为止,我的代码如下所示:

import java.util.Scanner;

class q2_5{
  public static void main(String args[]){

    Scanner input = new Scanner(System.in);
    int s=0;//seconds
    int m=0;//minutes
    int h=0;//hour
    System.out.println("how many seconds?");
    s=input.nextInt();
    if(s >= 60){
      m=s/60;
    } if(m>=60){
      h=m/60;
    }
    System.out.println(s + "s = " + h + " h " + m + " m " + s + "s ");
  }
}

好的,所以我必须将 s,m,h 初始化为 0,否则我在 if 语句中遇到问题,所以我将其设置为 0,因为我可以稍后更改它:) 好的。所以这个程序现在的问题是,如果我输入 3603,我会得到这个输出:3603s = 1 h 60 m 3603s,如果我输入 3600,我会得到这个: 3600s = 1 h 60 m 3600s,但输出应该分别是 3603s = 1h 0m 3s 和 3600s = 1h 0m 0s。有关如何解决此问题的任何提示/建议/解决方案? :D 提前谢谢!

【问题讨论】:

  • 您发布的代码都是左对齐的,因此无法阅读。我试图解决这个问题。以后请自行修复。如果我们无法阅读您的代码,我们无法理解,也无法提供帮助。

标签: java


【解决方案1】:

我的解决办法是:

 String secToTime(int sec) {
    int second = sec % 60;
    int minute = sec / 60;
    if (minute >= 60) {
        int hour = minute / 60;
        minute %= 60;
        return hour + ":" + (minute < 10 ? "0" + minute : minute) + ":" + (second < 10 ? "0" + second : second);
    }
    return minute + ":" + (second < 10 ? "0" + second : second);
}

【讨论】:

  • 在区块if(minute &gt;= 60),应该是hour = minute/60
  • 谢谢@PantelisSopasakis
【解决方案2】:

显然,您正在做一些家庭作业/练习来学习 Java。但仅供参考,在实际工作中,您不需要进行这些计算和操作。有一个类。

java.time.Duration

在一段时间内,使用Duration 类。不需要你做数学。

Duration d = Duration.ofSeconds( s );

表示未附加到时间线的时间跨度的字符串的标准 ISO 8601 格式接近于您想要的输出:PnYnMnDTnHnMnS 其中 P 标记开始,T 将任何年-月-日与任何小时分开- 分 - 秒。所以一个半小时是PT1H30M

java.time 类在解析或生成字符串时默认使用 ISO 8601 格式。

String output = d.toString() ;

在 Java 9 及更高版本中,您可以调用 to…Part 方法来检索小时、分钟和秒的各个部分。


关于java.time

java.time 框架内置于 Java 8 及更高版本中。这些类取代了麻烦的旧 legacy 日期时间类,例如 java.util.DateCalendarSimpleDateFormat

要了解更多信息,请参阅Oracle Tutorial。并在 Stack Overflow 上搜索许多示例和解释。规格为JSR 310

Joda-Time 项目现在位于maintenance mode,建议迁移到java.time 类。

您可以直接与您的数据库交换 java.time 对象。使用符合JDBC 4.2 或更高版本的JDBC driver。不需要字符串,不需要java.sql.* 类。 Hibernate 5 & JPA 2.2 支持 java.time

从哪里获取 java.time 类?

【讨论】:

【解决方案3】:

Java 8 为日期和时间操作带来了出色的 API。

import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;

public class Converter {

    DateTimeFormatter formatter = DateTimeFormatter.ofPattern("HH:mm:ss");

    public String convert(int seconds) {
        LocalTime time = LocalTime.MIN.plusSeconds(seconds).toLocalTime();

        return formatter.format(time);
    }
}

基本上,此代码将秒数添加到支持的最小日期时间,自然,HH:mm:ss 等于 00:00:00。

由于您只对时间感兴趣,您可以通过调用 toLocalTime() 来提取它并根据需要对其进行格式化。

【讨论】:

  • 为什么不直接去LocalTime 而不用LocalDateTime 来缩短它呢? LocalTime.MIN.plusHours( h ).plusMinutes( m ).plusSeconds( s )
  • 在这种情况下不需要DateTimeFormatter。所需的输出采用标准 ISO 8601 格式。 java.time 类在解析和生成字符串时默认使用 ISO 8601 格式。 myLocalTime.toString() 所以你的整个代码可以简化为:LocalTime.MIN.plusHours( h ).plusMinutes( m ).plusSeconds( s ).toString() 不需要子程序。
  • @BasilBourque,你是对的。我刚刚更新了代码。关于格式化程序,虽然问题要求使用特定格式,但为了完整性,以防其他人对不同格式感兴趣。
【解决方案4】:

您可以在一行中完成所有操作:

System.out.println((s/3600) + ' hours ' + ((s/60)%60) + ' minutes ' + (s%60) + ' seconds');

【讨论】:

    【解决方案5】:

    您从未更改过s 的值。一个快速的解决方法是s = s - (h*3600 + m*60)

    编辑:t = s - (h*3600 + m*60)

    【讨论】:

    • 太棒了,我试过了,但问题是现在我做对了,但是如果我输入 3600,就像它说的 0s = 1h 0m 0s?我需要做一个额外的变量吗?
    • 然后将 s 更改为 t 之类的其他名称。
    • 呵呵,谢谢伙计!顺便说一句,它的 t = s + (h*3600 + m*60) 不是 t = s - (h*3600 + m*60) :D
    【解决方案6】:

    每次设置下限时,请尝试从第一项中减去。

    class Test{
      public static void main(String args[]){
    
        Scanner input = new Scanner(System.in);
        int s=0;//seconds
        int m=0;//minutes
        int h=0;//hour
        System.out.println("how many seconds?");
        s=input.nextInt();
        if(s >= 60){
          m=s/60;
          s = s- m*60;
        } if(m>=60){
          h=m/60;
          m = m - h*60;
        }
        System.out.println(s + "s = " + h + " h " + m + " m " + s + "s ");
      }
    }
    

    顺便说一句,请记住关闭您的input 扫描仪!像这样:input.close()接近程序的结尾。

    【讨论】:

      【解决方案7】:

      你应该试试这个

      import java.util.Scanner;
      public class Time_converter {
      
      
          public static void main(String[] args) 
          {
      
              Scanner input = new Scanner (System.in);
              int seconds;
              int minutes ;
              int hours;
              System.out.print("Enter the number of seconds : ");
              seconds = input.nextInt();
              hours = seconds / 3600;
              minutes = (seconds%3600)/60;
              int seconds_output = (seconds% 3600)%60;
      
      
              System.out.println("The time entered in hours,minutes and seconds is:");
              System.out.println(hours  + " hours :" + minutes + " minutes:" + seconds_output +" seconds"); 
          }
      
      }
      

      【讨论】:

        【解决方案8】:
        import java.util.Scanner;
        
        public class Prg2 {
        
            public static void main(String[] args) {
                int seconds = 0;
                int hours = 0;
                int minutes = 0;
                int secondsDisp = 0;
                Scanner in = new Scanner(System.in);
                System.out.println("Enter the number of seconds: ");
                seconds = in.nextInt();
                hours = seconds/3600;
                minutes = (seconds - (hours*3600))/60;
                secondsDisp = ((seconds - (hours*3600)) - (minutes * 60));
                System.out.println(seconds + " seconds equals " + hours + " hours, " + minutes + " minutes and " + secondsDisp + " seconds");
            }
        }
        

        【讨论】:

          【解决方案9】:
          class time{
              public static void main (String args[]){
                  int duration=1500;
                   String testDuration = "";
          
                  if(duration < 60){
                      testDuration = duration + " minutes";
                  }
                  else{
          
                      if((duration / 60)<24)
                      {
                          if((duration%60)==0){
                              testDuration = (duration / 60) + " hours";
                          }
                          else{
                      testDuration = (duration / 60) + " hours," + (duration%60) + " minutes";
                          }
                      }
                      else{
          
                          if((duration%60)==0){
                              if(((duration/60)%24)==0){
                                  testDuration = ((duration / 24)/60) + " days,";
          
                              }
                              else{
                              testDuration = ((duration / 24)/60) + " days," + (duration/60)%24 +"hours";
                              }
                          }
                              else{
                          testDuration = ((duration / 24)/60) + " days," + (duration/60)%24 +"hours"+ (duration%60) + " minutes";
                              }
                      }
                  }
          
                  System.out.println(testDuration);
              }
          }
          

          【讨论】:

          • 这假设输入是分钟,您将其转换为天:小时:分钟,这不是问题。不过,您可以轻松修改它以解决确切的问题。
          【解决方案10】:

          一个更简单的解决方案是:

          T = in.nextInt();
          
          int s = T%60;
          
          int minute = T/60;
          
          int m = minute%60;
          
          int h = minute/60;
          
          return h+"h"+m+"m"+s+"s";
          

          【讨论】:

            【解决方案11】:

            使用此代码:

            import java.util.Scanner;
            class q2_5{
            public static void main(String args[]){
            
            Scanner input = new Scanner(System.in);
            int s=0;//seconds
            int m=0;//minutes
            int h=0;//hour
            int s_input=0;
            System.out.println("how many seconds?");
            s_input=input.nextInt();
            s=s_input%60;
            if(s >= 60){
            
            m=s_input/60;
            }if(m>=60){
            
            h=m/60;
            m=m%60;
            }
            System.out.println(s + "s = " + h + " h " + m + " m " + s + "s ");
             }
            }
            

            【讨论】:

              猜你喜欢
              • 2015-06-02
              • 2021-10-29
              • 1970-01-01
              • 2012-01-06
              • 2013-05-05
              相关资源
              最近更新 更多