如何在j中将分钟转换为小时和分钟(hh:mm)
我需要在Java中将分钟转换为小时和分钟。 例如260应该是4:20。 任何人都可以帮助我如何进行转换。
lakshmi asked 2019-11-08T21:13:11Z
18个解决方案
167 votes
如果您的时间在一个名为t的变量中
int hours = t / 60; //since both are ints, you get an int
int minutes = t % 60;
System.out.printf("%d:%02d", hours, minutes);
再简单不过了
Federico klez Culloca answered 2019-11-08T21:13:27Z
15 votes
TL;博士
Duration.ofMinutes( 260L )
.toString()
PT4H20M
… 要么 …
LocalTime.MIN.plus(
Duration.ofMinutes( 260L )
).toString()
04:20
YearQuarter
java.time类包括一对代表时间跨度的类。 YearQuarter类用于小时-分钟-秒,而YearQuarter用于类-年-月-日。
Duration d = Duration.ofMinutes( 260L );
ISO 8601
ISO 8601标准定义了日期时间值的文本格式。 对于未附加到时间轴的时间跨度,标准格式为YearQuarter。P标志着开始,而T将年-月-日与小时-分钟-秒分开。 所以一个半小时是PT1H30M。
默认情况下,java.time类使用ISO 8601格式来解析和生成字符串。 YearQuarter和YearQuarter类使用此特定的标准格式。 因此,只需致电YearQuarter。
String output = d.toString();
PT4H20M
对于备用格式,请使用YearQuarter方法在Java 9和更高版本(而不是Java 8)中构建自己的String。 或参阅此答案以了解使用正则表达式操作ISO 8601格式的字符串。
YearQuarter
我强烈建议您使用标准ISO 8601格式,而不要使用YearQuarter极为模棱两可的时钟格式。但是,如果您坚持要这样做,可以通过使用YearQuarter类来获得这种效果。 如果您的时间不超过24小时,则此方法有效。
LocalTime hackUseOfClockAsDuration = LocalTime.MIN.plus( d );
String output = hackUseOfClockAsDuration.toString();
04:20
关于java.time
java.time框架内置于Java 8及更高版本中。 这些类取代了麻烦的旧的传统日期时间类,例如YearQuarter、YearQuarter和YearQuarter。
现在处于维护模式的Joda-Time项目建议迁移到java.time类。
要了解更多信息,请参阅Oracle教程。 并搜索Stack Overflow以获取许多示例和解释。 规范是JSR 310。
从哪里获取java.time类?
Java SE 8和SE 9及更高版本内置。
带有捆绑实现的标准Java API的一部分。
Java 9增加了一些小功能和修复。
Java SE 6和SE 7许多java.time功能都被反向移植到ThreeTen-Backport中的Java 6和7。
Android的ThreeTenABP项目专门针对Android改编了ThreeTen-Backport(如上所述)。
请参阅如何使用ThreeTenABP…。
ThreeTen-Extra项目使用其他类扩展了java.time。 该项目为将来可能在java.time中添加内容提供了一个试验场。 您可能会在这里找到一些有用的类,例如YearQuarter,YearQuarter,YearQuarter等。
Basil Bourque answered 2019-11-08T21:15:28Z
11 votes
您也可以使用TimeUnit类。 您可以定义private static final String FORMAT = "%02d:%02d:%02d";可以采用类似以下方法:
public static String parseTime(long milliseconds) {
return String.format(FORMAT,
TimeUnit.MILLISECONDS.toHours(milliseconds),
TimeUnit.MILLISECONDS.toMinutes(milliseconds) - TimeUnit.HOURS.toMinutes(
TimeUnit.MILLISECONDS.toHours(milliseconds)),
TimeUnit.MILLISECONDS.toSeconds(milliseconds) - TimeUnit.MINUTES.toSeconds(
TimeUnit.MILLISECONDS.toMinutes(milliseconds)));
}
walters answered 2019-11-08T21:15:58Z
8 votes
使用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();
}
Athar Iqbal answered 2019-11-08T21:16:23Z
3 votes
可以这样做
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();
Priyanka answered 2019-11-08T21:16:47Z
1 votes
Minutes mod 60将提供数小时的剩余时间。
[http://www.cafeaulait.org/course/week2/15.html]
Ryre answered 2019-11-08T21:17:18Z
1 votes
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);
developer answered 2019-11-08T21:17:37Z
1 votes
试试这段代码:
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");
}
}
Jayaramachandran P answered 2019-11-08T21:17:56Z
1 votes
给定输入时间(以秒为单位),您可以像这样转换为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,因为输入可以超过86 400秒,也就是24小时。
如果要用hh:mm:ss表示输入的总时间,则可以避免使用formatHelper。
我希望它有所帮助。
LightinDarknessJava answered 2019-11-08T21:18:35Z
1 votes
在斯威夫特
func timeStringFrom(minutes:Double) -> String {
return "\(Int(minutes/60)):\(Int(minutes) % 60)"
}
iCyberPaul answered 2019-11-08T21:18:55Z
1 votes
我在项目中使用此功能:
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;
}
Shohan Ahmed Sijan answered 2019-11-08T21:19:20Z
1 votes
(在Kotlin中)如果要将答案放入TextView或其他内容中,则可以使用字符串资源:
%02d:%02d
然后,您可以使用此String资源,然后使用以下命令在运行时设置文本:
private fun setTime(time: Int) {
val hour = time / 60
val min = time % 60
main_time.text = getString(R.string.time, hour, min)
}
Anthony Cannon answered 2019-11-08T21:19:51Z
0 votes
您在回复中被遗忘了发言:
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。
user2389203 answered 2019-11-08T21:20:28Z
0 votes
使用以下代码计算分钟数:
long difference = new Date().getTime() - user.getUpdatedDate().getTime();
int minutes = (int) TimeUnit.MILLISECONDS.toMinutes(difference);
Pritam Panja answered 2019-11-08T21:20:53Z
0 votes
int mHours = t / 60; //since both are ints, you get an int
int mMinutes = t % 60;
System.out.printf("%d:%02d", "" +mHours, "" +mMinutes);
Jigar Patel answered 2019-11-08T21:21:11Z
0 votes
import java.util.Scanner;
public class Time{
public static void main(String[]args){
int totMins=0;
int hours=0;
int mins=0;
Scanner sc= new Scanner(System.in);
System.out.println("Enter the time in mins: ");
totMins= sc.nextInt();
hours=(int)(totMins/60);
mins =(int)(totMins%60);
System.out.printf("%d:%d",hours,mins);
}
}
Janeth Fernando answered 2019-11-08T21:21:29Z
0 votes
这是我将second,millisecond转换为day,hour,minute,second的函数
public static String millisecondToFullTime(long millisecond) {
return timeUnitToFullTime(millisecond, TimeUnit.MILLISECONDS);
}
public static String secondToFullTime(long second) {
return timeUnitToFullTime(second, TimeUnit.SECONDS);
}
public static String timeUnitToFullTime(long time, TimeUnit timeUnit) {
long day = timeUnit.toDays(time);
long hour = timeUnit.toHours(time) % 24;
long minute = timeUnit.toMinutes(time) % 60;
long second = timeUnit.toSeconds(time) % 60;
if (day > 0) {
return String.format("%dday %02d:%02d:%02d", day, hour, minute, second);
} else if (hour > 0) {
return String.format("%d:%02d:%02d", hour, minute, second);
} else if (minute > 0) {
return String.format("%d:%02d", minute, second);
} else {
return String.format("%02d", second);
}
}
测试
public static void main(String[] args) {
System.out.println("60 => " + secondToFullTime(60));
System.out.println("101 => " + secondToFullTime(101));
System.out.println("601 => " + secondToFullTime(601));
System.out.println("7601 => " + secondToFullTime(7601));
System.out.println("36001 => " + secondToFullTime(36001));
System.out.println("86401 => " + secondToFullTime(86401));
}
产量
60 => 1:00
101 => 1:41
601 => 10:01
7601 => 2:06:41
36001 => 10:00:01
86401 => 1day 00:00:01
希望对你有帮助
Phan Van Linh answered 2019-11-08T21:21:59Z
0 votes
我们可以使用Duration,但它需要Android O或更高版本。
这是我的看法:
private fun translateMinsToString24hrsFormat(autoSettlementTimeInMins : Int): String {
val hours = (autoSettlementTimeInMins / 60).toString()
val minutes = (autoSettlementTimeInMins % 60).toString()
return "$hours:$minutes"
}
Haomin answered 2019-11-08T21:22:31Z