如何将分钟转换为天,小时,分钟

时间:2010-05-01 18:24:58

标签: java time duration

如何在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;
 }

我尝试了这个,但它不起作用

感谢

13 个答案:

答案 0 :(得分:29)

更短的方式。 (假设时间&gt; = 0)

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

答案 1 :(得分:15)

如果你想自己做,那就走另一条路。

  1. 将数字除以60 * 24.(这将获得天数。)
  2. 将剩余部分除以60.(这将为您提供小时数。)
  3. #2的剩余部分是分钟数。

答案 2 :(得分:9)

如果使用Java 6,则TimeUnit枚举可能很有用。例如:

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

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

实际上,TimeUnit可以从Java 5获得,但在版本6中添加了更多的单元。

- EDIT-- 现在我更了解你的问题,使用除了Romain的响应中的除法和余数方法。我的提示仅适用于转换为单个时间单位。

答案 3 :(得分:5)

答案是:

 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;
 }

您对此代码有何看法?

答案 4 :(得分:4)

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

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

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

答案 5 :(得分:2)

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 ";
        }

        return result;
    }

    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;
}

答案 6 :(得分:2)

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。

答案 7 :(得分:2)

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

function SendDataToGlobalStore(setProducts) {
  db
    .collection("products")
    .orderBy('timestamp', 'desc')
    .onSnapshot(dataSnapshot => {
      dataSnapshot
        .docs
        .map(doc => {
          db 
            .collection("products")
            .doc(doc.id)
            .collection("images")
            .onSnapshot(imagesSnapshot => {
               imagesSnapshot.docs.map(doc => {
                 const image = doc.data().image
                 db
                   .collection("products")
                   .orderBy('timestamp', 'desc')
                   .onSnapshot(dataSnapshot => {
                     setProducts( dataSnapshot.docs.map((doc) => ({
                       product: {
                          id: doc.id,
                          name: doc.data().name,
                          price: doc.data().price,
                          description: doc.data().description,
                          images: image
                       }
                     })))
                  })
               })
            })
        }) 
     })
  })
}

答案 8 :(得分:0)

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

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";
        }
    }
}

答案 9 :(得分:0)

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);
    }
}

答案 10 :(得分:0)

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`
    }
}

答案 11 :(得分:0)

(时间/ 24/60).toFixed(0)+“:” +(时间/ 60%24).toFixed(0)+':'+(时间%60) 最好吗

答案 12 :(得分:0)

只要使方法返回字符串并取(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;

}