如何在java中将分钟转换为小时和分钟(hh:mm)
我需要将分钟转换成java中的小时和分钟。 例如260应该是4:20。 任何人都可以帮助我如何转换它。
如果你的时间在一个叫做t的variables中 
 int hours = t / 60; //since both are ints, you get an int int minutes = t % 60; System.out.printf("%d:%02d", hours, minutes); 
它不能变得更容易
您也可以使用TimeUnit类。 你可以定义
private static final String FORMAT =“%02d:%02d:%02d”;
可以有如下的方法:
 public static String parseTime(long milliseconds){
      返回String.format(FORMAT,
               TimeUnit.MILLISECONDS.toHours(毫秒)
               TimeUnit.MILLISECONDS.toMinutes(毫秒) -  TimeUnit.HOURS.toMinutes(
               TimeUnit.MILLISECONDS.toHours(毫秒)),
               TimeUnit.MILLISECONDS.toSeconds(毫秒) -  TimeUnit.MINUTES.toSeconds(
               TimeUnit.MILLISECONDS.toMinutes(毫秒)));
    }
TL;博士
 Duration.ofMinutes( 260L ) .toString() 
PT4H20M
… 要么 …
 LocalTime.MIN.plus( Duration.ofMinutes( 260L ) ).toString() 
04:20
 Duration 
  java.time类包含一对表示时间跨度的类。  Duration类是几分钟 – 秒,而时段是几年 – 几月 – 天。 
 Duration d = Duration.ofMinutes( 260L ); 
ISO 8601
  ISO 8601标准定义了date时间值的文本格式。 对于时间跨度不连接的时间跨度, 标准格式是PnYnMnDTnHnMnS 。  P标志着开始, T将时间 – 月 – 日与时 – 分 – 秒分开。 所以一个半小时是PT1H30M 。 
  java.time类默认使用ISO 8601格式来parsing和生成string。  “ Duration和“ Period类使用此特定的标准格式 所以简单地调用toString 。 
 String output = d.toString(); 
PT4H20M
 对于备用格式,使用Duration::to…Part方法在Java 9及更高版本( 而不是 Java 8)中构build您自己的string。 或者看到这个答案使用正则expression式来操纵ISO 8601格式的string。 
 LocalTime 
 我强烈build议使用标准的ISO 8601格式,而不是04:20的非常模糊和混乱的时钟格式。 但是如果你坚持,你可以通过使用LocalTime类来破解这个效果。 如果你的持续时间不超过24小时,这是有效的。 
 LocalTime hackUseOfClockAsDuration = LocalTime.MIN.plus( d ); String output = hackUseOfClockAsDuration.toString(); 
04:20
关于java.time
  java.time框架内置于Java 8及更高版本中。 这些类取代了麻烦的旧的遗留date时间类,如java.util.Date , Calendar和SimpleDateFormat 。 
Joda-Time项目现在处于维护模式 ,build议迁移到java.time类。
要了解更多信息,请参阅Oracle教程 。 并search堆栈溢出了很多例子和解释。 规范是JSR 310 。
从何处获取java.time类?
-   Java SE 8和SE 9及更高版本
- 内置。
- 带有捆绑实现的标准Java API的一部分。
- Java 9增加了一些小function和修复。
 
-   Java SE 6和SE 7
- 大部分的java.timefunction都被移植到了ThreeTen-Backport中的 Java 6&7中。
 
-   Android的
- ThreeTenABP项目专门针对Android,采用了ThreeTen-Backport (上文提到)。
- 请参阅如何使用ThreeTenABP …。
 
  ThreeTen-Extra项目将java.time扩展到其他类。 这个项目是未来可能增加java.time的一个试验场。 你可能会在这里find一些有用的类,比如Interval , YearWeek , YearQuarter 等等 。 
可以这样做
  int totalMinutesInt = Integer.valueOf(totalMinutes.toString()); int hours = totalMinutesInt / 60; int hoursToDisplay = hours; if (hours > 12) { hoursToDisplay = hoursToDisplay - 12; } int minutesToDisplay = totalMinutesInt - (hours * 60); String minToDisplay = null; if(minutesToDisplay == 0 ) minToDisplay = "00"; else if( minutesToDisplay < 10 ) minToDisplay = "0" + minutesToDisplay ; else minToDisplay = "" + minutesToDisplay ; String displayValue = hoursToDisplay + ":" + minToDisplay; if (hours < 12) displayValue = displayValue + " AM"; else displayValue = displayValue + " PM"; return displayValue; } catch (Exception e) { LOGGER.error("Error while converting currency."); } return totalMinutes.toString(); 
 使用java.text.SimpleDateFormat将分钟转换为小时和分钟 
 SimpleDateFormat sdf = new SimpleDateFormat("mm"); try { Date dt = sdf.parse("90"); sdf = new SimpleDateFormat("HH:mm"); System.out.println(sdf.format(dt)); } catch (ParseException e) { e.printStackTrace(); } 
分钟模式60将提供几分钟剩余的时间。
 long d1Ms=asa.getTime(); long d2Ms=asa2.getTime(); long minute = Math.abs((d1Ms-d2Ms)/60000); int Hours = (int)minute/60; int Minutes = (int)minute%60; stUr.setText(Hours+":"+Minutes); 
试试这个代码:
 import java.util.Scanner; public class BasicElement { public static void main(String[] args){ Scanner input = new Scanner(System.in); int hours; System.out.print("Enter the hours to convert:"); hours =input.nextInt(); int d=hours/24; int m=hours%24; System.out.println(d+"days"+" "+m+"hours"); } } 
给定input秒,你可以转换为格式hh:mm:ss像这样:
 int hours; int minutes; int seconds; int formatHelper; int input; //formatHelper maximum value is 24 hours represented in seconds formatHelper = input % (24*60*60); //for example let's say format helper is 7500 seconds hours = formatHelper/60*60; minutes = formatHelper/60%60; seconds = formatHelper%60; //now operations above will give you result = 2hours : 5 minutes : 0 seconds; 
我已经使用formatHelper,因为input可以超过86 400秒,即24小时。
如果你想要你input的总时间由hh:mm:ss代表,你可以避免使用formatHelper。
我希望它有帮助。
我为我的项目使用这个function:
  public static String minuteToTime(int minute) { int hour = minute / 60; minute %= 60; String p = "AM"; if (hour >= 12) { hour %= 12; p = "PM"; } if (hour == 0) { hour = 12; } return (hour < 10 ? "0" + hour : hour) + ":" + (minute < 10 ? "0" + minute : minute) + " " + p; } 
你在回应中被遗忘了:
 int hours = Math.floor(t / 60); int minutes = t % 60; System.out.printf("%d:%02d", hours, minutes); 
没有它,你的结果在某些情况下是错误的,例如:
 t = 525 hours = 8.75 
所以没有Math.floor我们有这个结果:9h45而不是8h45。
使用此代码计算分钟数:
 long difference = new Date().getTime() - user.getUpdatedDate().getTime(); int minutes = (int) TimeUnit.MILLISECONDS.toMinutes(difference); 
在Swift中
 func timeStringFrom(minutes:Double) -> String { return "\(Int(minutes/60)):\(Int(minutes) % 60)" } 
 int mHours = t / 60; //since both are ints, you get an int int mMinutes = t % 60; System.out.printf("%d:%02d", "" +mHours, "" +mMinutes);