假设我有110秒的时间要转换为01:50或00:01:50之类的。我怎么才能在欢乐时光里做到这一点?我将这个数字加载到秒内,但是toString不会为我进行转换。
发布于 2014-03-15 06:34:54
LocalTime time = new LocalTime(0, 0); // midnight
time = time.plusSeconds(110);
String output = DateTimeFormat.forPattern("HH:mm:ss").print(time);
System.out.println(output); // 00:01:50注意,这个答案在少于一整天(86400)的秒内有效。如果您有更大的数字,那么最好使用一个持续时间格式化程序(在Joda-Time中称为PeriodFormatter) --也请参见@Isaksson的正确答案。
发布于 2014-03-15 06:58:38
您可以为您的格式构建一个格式化程序并使用它。若要获取以秒为单位的时间(仅为分钟/秒),请使用normalizedStandard();
PeriodFormatter myFormat =
new PeriodFormatterBuilder()
.printZeroAlways().minimumPrintedDigits(2).appendMinutes()
.appendSeparator(":")
.printZeroAlways().minimumPrintedDigits(2).appendSeconds()
.toFormatter();
Period period = Period.seconds(110).normalizedStandard();
System.out.println(myFormat.print(period));
> 01:50发布于 2015-12-22 22:36:19
public static void main(String[] args) {
Date dt = new Date();
String date = String.format("%tH:%tM", dt, dt);
System.out.println(date);
// or System.out.printf("%tH:%tM", dt, dt);
}https://stackoverflow.com/questions/22420335
复制相似问题