【问题标题】:how to convert minutes to days,hours,minutes如何将分钟转换为天、小时、分钟
【发布时间】:2011-02-14 14:48:00
【问题描述】:

如何在java中将分钟转换为天小时和分钟(我们这里有一周,7天)

 public String timeConvert(int time){
   String t = "";

   int h = 00;
   int m = 00;

  // h= (int) (time / 60);
  // m = (int) (time % 60);

  // if(h>=24) h=00;

   if((time>=0) && (time<=24*60)){
      h= (int) (time / 60);
      m = (int) (time % 60);
   }else if((time>24*60) && (time<=24*60*2)){
       h= (int) (time / (1440));
      m = (int) (time % (1440));
   }else if((time>24*60*2) && (time<=24*60*3)){
       h= (int) (time / (2880));
      m = (int) (time % (2880));
   }else if((time>24*60*3) && (time<=24*60*4)){
       h= (int) (time / (2880*2));
      m = (int) (time % (2880*2));
   }else if((time>24*60*4) && (time<=24*60*5)){
       h= (int) (time / (2880*3));
      m = (int) (time % (2880*3));
   }else if((time>24*60*5) && (time<=24*60*6)){
       h= (int) (time / (2880*4));
      m = (int) (time % (2880*4));
   }else if((time>24*60*6) && (time<=24*60*7)){
       h= (int) (time / (2880*5));
      m = (int) (time % (2880*5));
   }

   t =h+":"+m ;
   return t;
 }

我试过了,但是不行

谢谢

【问题讨论】:

    标签: java time duration


    【解决方案1】:

    更短的方法。 (假设时间 >= 0)

     public String timeConvert(int time) { 
       return time/24/60 + ":" + time/60%24 + ':' + time%60;
     }
    

    【讨论】:

    • 效果很好,谢谢。为了防止小数,它应该是Math.floor(time/24/60) + ":" + Math.floor(time/60%24) + ':' + Math.floor(time%60)
    • @Dr.DS 整数运算在 Java 中不会产生小数。 time/60 始终为 int 值。例如1/60 == 0 在 Java 中。
    • 对不起.. 我刚刚注意到这个答案有一个 java 标签。我在为 javascript 搜索类似内容时找到了您的答案。所以评论了一个更新,牢记javascript。无论如何...它帮助我朝着正确的方向前进:-)
    【解决方案2】:

    如果你想自己做,就另当别论。

    1. 将数字除以 60 * 24。(即得到天数。)
    2. 将余数除以 60。(这将给出小时数。)
    3. #2 的余数是分钟数。

    【讨论】:

    • 但是我有一天以上的时间,我的分钟从 0 开始,到 10080 结束,使用这个算法,我会像 136:30 一样
    • Romain 的方法工作得很好,只要你在前两种情况下都取商和余数。如果您有 1600 分钟并除以 (60 * 24),则商为 1,余数为 160,然后除以 60。商为 2,余数为 40。因此 1600 分钟等于 1 天,2小时,40 分钟。
    【解决方案3】:

    如果您使用 Java 6,TimeUnit 枚举会很有用。例如:

    TimeUnit.HOURS.convert(10, TimeUnit.DAYS)
    

    此静态调用将 10 天转换为小时单位,并返回 240。您可以使用从 NANOSECONDS 开始并以 DAYS 结束的时间单位。

    实际上 TimeUnit 从 Java 5 开始可用,但在版本 6 中添加了更多单位。

    --编辑-- 既然我更好地理解了您的问题,请使用除法和余数方法,就像 Romain 的回答一样。我的提示仅对转换为单个时间单位有用。

    【讨论】:

    • 谢谢,但我不知道如何使用它,没有关于它的文档
    • @tuxou : 有 javadoc
    【解决方案4】:

    答案是:

     public String timeConvert(int time){
       String t = "";
    
      int j = time/(24*60);
      int h= (time%(24*60)) / 60;
      int m = (time%(24*60)) % 60;
    
    
    
       t =j + ":" + h + ":" + m;
       return t;
     }
    

    你觉得这段代码怎么样?

    【讨论】:

    • 整体方法不错,但我对改进风格的建议是:a) 不要使用单字母变量名 b) 避免预先声明具有从未使用过的值的变量,最好这样做,例如"int hour = (time%(24*60)) / 60;"
    • 我不同意 b 点,Mikira 建议您将尽可能多的代码塞进一行中。但是,如果您像 Eddinho 所做的那样将变量分成小步骤,那么其他人就更容易单步执行您的代码并查看发生了什么。至于a)点,这是一个好点。我也会远离单字母变量,除非它们是临时的,而我想出一个合适的名字。在这些情况下,我会对变量的类型进行描述,例如s 代表字符串,i 代表 int,dt 代表 DataTable 等。不过,Eddinho 是一个很好的解决方案。它有效,而且非常优雅!
    【解决方案5】:

    1) 您的代码是重复的。在我看来,这是错误代码的标志。

    2) 除数不应随天数而变化,因为天数与一小时的分钟数关系不大。

    除此之外,看看 Romain Hippeau 的方法,他告诉你怎么做。

    【讨论】:

      【解决方案6】:

      1 天 2 小时 5 分钟

      public static String convertToDaysHoursMinutes(long minutes) {
      
          int day = (int)TimeUnit.MINUTES.toDays(minutes);
          long hours = TimeUnit.MINUTES.toHours(minutes) - (day *24);
          long minute = TimeUnit.MINUTES.toMinutes(minutes) - (TimeUnit.MINUTES.toHours(minutes)* 60);
      
          String result = "";
      
          if (day != 0){
              result += day;
              if (day == 1){
                  result += " day ";
              }
              else{
                  result += " days ";
              }
          }
      
          if (hours != 0){
              result += hours;
      
              if (hours == 1){
                  result += " hr ";
              }
              else{
                  result += " hrs ";
              }
          }
      
          if (minute != 0){
              result += minute;
      
              if (minute == 1){
                  result += " min";
              }
              else{
                  result += " mins";
              }
          }
      
          return result;
      }
      

      【讨论】:

        【解决方案7】:

        Java-9 java.time 解决方案:

        import java.time.Duration;
        
        public class Main {
            public static void main(String[] args) {
                // Test
                System.out.println(minutesToDaysHoursMinutes(10080));
                System.out.println(minutesToDaysHoursMinutes(1600));
            }
        
            public static String minutesToDaysHoursMinutes(int time) {
                Duration d = Duration.ofMinutes(time);
                long days = d.toDaysPart();
                long hours = d.toHoursPart();
                long minutes = d.toMinutesPart();
                return String.format("%d Day(s) %d Hour(s) %d Minute(s)", days, hours, minutes);
            }
        }
        

        输出:

        7 Day(s) 0 Hour(s) 0 Minute(s)
        1 Day(s) 2 Hour(s) 40 Minute(s)
        

        Java-8 java.time 解决方案:

        import java.time.Duration;
        
        public class Main {
            public static void main(String[] args) {
                // Test
                System.out.println(minutesToDaysHoursMinutes(10080));
                System.out.println(minutesToDaysHoursMinutes(1600));
            }
        
            public static String minutesToDaysHoursMinutes(int time) {
                Duration d = Duration.ofMinutes(time);
                long days = d.toDays();
                long hours = d.toHours() % 24;
                long minutes = d.toMinutes() % 60;
                return String.format("%d Day(s) %d Hour(s) %d Minute(s)", days, hours, minutes);
            }
        }
        

        输出:

        7 Day(s) 0 Hour(s) 0 Minute(s)
        1 Day(s) 2 Hour(s) 40 Minute(s)
        

        Trail: Date Time 了解现代日期时间 API。

        【讨论】:

          【解决方案8】:

          如果有人想使用 JavaScript 的公认答案,时间将以小数显示。为了删除这些小数,您可以使用以下代码:

          function timeConvert(time)
          { 
             return parseInt(time/24/60) + " days," + parseInt(time/60%24) + ' hours,' + parseInt(time%60) + " minutes";
          }
          

          【讨论】:

            【解决方案9】:

            我正在使用此代码。它也可以提供帮助。

            private String getText(int minutes){
            
                int weeks = minutes / 10080;
                int aboveWeeks = minutes % 10080;
                int days = aboveWeeks / 1440;
                int aboveDays = aboveWeeks % 1440;
                int hours = aboveDays / 60;
                int aboveHours = aboveDays % 60;
                int minute = aboveHours / 60;
            
                if(weeks > 0 && days > 0) {
                    if(weeks > 1 && days > 1){
                        return weeks + " weeks " + days + " days before";
                    } else {
                        return weeks + " weeks " + days + " day before";
                    }
                } else if (weeks > 0){
                    if (weeks > 1){
                        return weeks + " weeks before";
                    } else {
                        return weeks + " week before";
                    }
                } else if(days > 0 && hours > 0){
                    if(days > 1 && hours > 1){
                        return days + " days " + hours + " hours before";
                    } else {
                        return days + " days " + hours + " hour before";
                    }
                } else if(days > 0){
                    if (days > 1){
                        return days + " days before";
                    } else {
                        return days + " day before";
                    }
                } else if(hours > 0 && minute > 0){
                    if(hours > 1 && minute > 1){
                        return hours + " hours " + minute + " minutes before";
                    } else {
                        return hours + " hours " + minute + " minute before";
                    }
                } else if(hours > 0){
                    if (hours > 1){
                        return hours + " hours before";
                    } else {
                        return hours + " hour before";
                    }
                } else {
                    if (minutes > 1){
                        return minutes + " minutes before";
                    } else {
                        return minutes + " minute before";
                    }
                }
            }
            

            【讨论】:

              【解决方案10】:
              class time{
                  public static void main (String args[]){
                      System.out.println("Hello");
                      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);
                  }
              }
              

              【讨论】:

                【解决方案11】:

                为了获得最佳可读性,它不会打印 0 天、0 小时或 0 分钟。 例如-(如果分钟=1500,那么它只会打印 1 天 1 小时)

                int 分钟=150;

                    StringBuilder sb=new StringBuilder();
                
                    int day=minute/1440;
                    int rem=minute%1440;
                    int hour=rem/60;
                    int Minute=rem%60;
                
                if(day>0)
                        sb.append(day+" day ");
                
                if(hour>0)
                        sb.append(hour+" hour ");
                
                if(Minute>0)
                        sb.append(Minute+" minute");
                
                    System.out.println(sb);
                

                【讨论】:

                  【解决方案12】:
                  const convertMinutesToDays = (minutes) => {
                      let hours
                      let days 
                      let restMinutes
                      const onedayMinutes = 1440 //24*60
                  
                      if(minutes < 60){
                          return `${minutes} Minutes` 
                      }else if(minutes > 60 && minutes < onedayMinutes){
                          hours = Math.floor(minutes/60)
                          restMinutes = minutes%60
                          return `${hours} Hours ${restMinutes} Minutes`
                      }else{
                          days = Math.floor((minutes/60)/24)
                          restMinutes = minutes % onedayMinutes
                          hours = Math.floor(restMinutes/60) 
                          restMinutes = restMinutes % 60
                          return `${days} Days ${hours} Hours ${restMinutes} Minutes`
                      }
                  }
                  

                  【讨论】:

                    【解决方案13】:

                    (time / 24 / 60).toFixed(0) + ":" +(time / 60 % 24).toFixed(0) + ':' + (time % 60) 最好用吗

                    【讨论】:

                    • 这不是 JavaScript;它是 Java。
                    【解决方案14】:

                    只需让方法返回字符串并获取(int minuts) String getTextOfTime(int minuts) {返回结果

                    String getTextOfTime(int minuts) {

                    int weeks = (-minuts / 10080).toInt();
                    int aboveWeeks = -minuts % 10080;
                    int days = (aboveWeeks / 1440).toInt();
                    int aboveDays = (aboveWeeks % 1440);
                    int hours = (aboveDays / 60).toInt();
                    int aboveHours = aboveDays % 60;
                    int minute = aboveHours;
                    
                    String result = "";
                    
                    if (weeks != 0) {
                      result += weeks.toString();
                      if (weeks == 1) {
                        result += " week ";
                      } else {
                        result += " weeks ";
                      }
                    }
                    
                    if (days != 0) {
                      result += days.toString();
                      if (days == 1) {
                        result += " day ";
                      } else {
                        result += " days ";
                      }
                    }
                    
                    if (hours != 0) {
                      result += hours.toString();
                    
                      if (hours == 1) {
                        result += " hr ";
                      } else {
                        result += " hrs ";
                      }
                    }
                    
                    if (minute != 0) {
                      result += minute.toString();
                    
                      if (minute == 1) {
                        result += " min";
                      } else {
                        result += " mins";
                      }
                    }
                    return result;
                    

                    }

                    【讨论】:

                    • 这个方法我很确定