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

发布于 2024-08-30 15:23:57 字数 1194 浏览 6 评论 0原文

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

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

谢谢

how to convert minutes into days hours and minutes in java ( we have a week here , 7 days )

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

I tried this but it dont work

thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(15

絕版丫頭 2024-09-06 15:23:57

一条更短的路。 (假设时间 >= 0)

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

A shorter way. (Assumes time >= 0)

 public String timeConvert(int time) { 
   return time/24/60 + ":" + time/60%24 + ':' + time%60;
 }
君勿笑 2024-09-06 15:23:57

如果您想自己执行此操作,请走另一条路。

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

If you want to do this yourself, go the other way.

  1. Divide the number by 60 * 24. (That will get the number of days.)
  2. Divide the remainder by 60. (That will give you number of hours.)
  3. The remainder of #2 is the number of minutes.
樱&纷飞 2024-09-06 15:23:57

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

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

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

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

- 编辑 -
现在我更好地理解了你的问题,请使用除法和余数方法,就像罗曼的回答一样。我的技巧仅适用于转换为单个时间单位。

If you use Java 6, TimeUnit enum can be useful. For example:

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

This static call converts 10 days into hour units, and returns 240. You can play with time units starting from NANOSECONDS and ending with DAYS.

Actually TimeUnit is available from Java 5, but in version 6 more units were added.

--EDIT--
Now that I understand better your question, use the division and remainder approach as in the response of Romain. My tip is useful only for conversion to a single time unit.

阪姬 2024-09-06 15:23:57

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 /强>。

Java-9 java.time Solution:

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

Output:

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

Java-8 java.time Solution:

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

Output:

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

Learn about the modern date-time API from Trail: Date Time.

羅雙樹 2024-09-06 15:23:57

答案是:

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

你觉得这段代码怎么样?

and the answer is :

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

what do you think about this code?

风渺 2024-09-06 15:23:57

1)你的代码是重复的。在我看来,这是糟糕代码的标志。

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

除此之外,看看 Romain Hippeau 的方法,他告诉你如何去做。

1) Your code is repetitive. This is a sign of bad code in my opinion.

2) The divisor shouldn't be changing with the number of days, because the number of days has little to do with the number of minutes in an hour.

Beyond that, look at Romain Hippeau's approach, he told you how to do it.

ぃ弥猫深巷。 2024-09-06 15:23:57

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

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

In case someone wants to use the accepted answer for JavaScript, time will be shown with decimals. In order to remove those decimals, you can use this code:

function timeConvert(time)
{ 
   return parseInt(time/24/60) + " days," + parseInt(time/60%24) + ' hours,' + parseInt(time%60) + " minutes";
}
心欲静而疯不止 2024-09-06 15:23:57

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

1 day 2 hrs 5 mins

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;
}
千年*琉璃梦 2024-09-06 15:23:57

我正在使用这个代码。它也有帮助。

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

i am using this code. it can also help.

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";
        }
    }
}
清醇 2024-09-06 15:23:57
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);
    }
}
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);
    }
}
过潦 2024-09-06 15:23:57

为了获得最佳可读性,它不会打印 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);

for best readability ,like it won't print 0 day or 0 hour or 0 minute.
e.g- (if minute=1500,then it will print only 1 day 1 hour)

int minute=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);
挽手叙旧 2024-09-06 15:23:57
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`
    }
}
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`
    }
}
↙厌世 2024-09-06 15:23:57

(时间 / 24 / 60).toFixed(0) + ":" +(时间 / 60 % 24).toFixed(0) + ':' + (时间 % 60)
做到最好

(time / 24 / 60).toFixed(0) + ":" +(time / 60 % 24).toFixed(0) + ':' + (time % 60)
Does it best

迟月 2024-09-06 15:23:57

只需让方法返回字符串并采取( int 分钟)
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;

}

just make method return string and take( int minuts)
String getTextOfTime(int minuts) {return result

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;

}

淡看悲欢离合 2024-09-06 15:23:57

对于那些寻找此逻辑的 SQL 版本的人:

其中 c.a_ARR 是开始日期时间,c.a_DEP 是结束日期时间

       CASE WHEN DATEDIFF(minute, c.a_ARR, c.a_DEP)/60/24 = 0.0
            THEN ''
            ELSE FORMAT(DATEDIFF(minute, c.a_ARR, c.a_DEP)/60/24,'0') + 'd'
       END +
       CASE WHEN DATEDIFF(minute, c.a_ARR, c.a_DEP)/60%24 = 0.0 
            THEN ''
            ELSE FORMAT(DATEDIFF(minute, c.a_ARR, c.a_DEP)/60%24,'0') + 'h'
       END +
       CASE WHEN DATEDIFF(minute, c.a_ARR, c.a_DEP)%60 = 0.0
            THEN ''
            ELSE FORMAT(DATEDIFF(minute, c.a_ARR, c.a_DEP)%60,'0') + 'm'
       END AS [Duration]

For those looking for an SQL version of this logic:

where c.a_ARR is a Start Datetime, c.a_DEP is an End DateTime

       CASE WHEN DATEDIFF(minute, c.a_ARR, c.a_DEP)/60/24 = 0.0
            THEN ''
            ELSE FORMAT(DATEDIFF(minute, c.a_ARR, c.a_DEP)/60/24,'0') + 'd'
       END +
       CASE WHEN DATEDIFF(minute, c.a_ARR, c.a_DEP)/60%24 = 0.0 
            THEN ''
            ELSE FORMAT(DATEDIFF(minute, c.a_ARR, c.a_DEP)/60%24,'0') + 'h'
       END +
       CASE WHEN DATEDIFF(minute, c.a_ARR, c.a_DEP)%60 = 0.0
            THEN ''
            ELSE FORMAT(DATEDIFF(minute, c.a_ARR, c.a_DEP)%60,'0') + 'm'
       END AS [Duration]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文