Reputation: 511
I want to convert seconds into HH:mm:ss
time format, so:
seconds = 3754
result = 10:25:40
I know about the conventional approach of dividing it by 3600 to get hours an so on, but was wondering if I can achieve this through Java API?
Upvotes: 6
Views: 9455
Reputation: 338266
LocalTime
The java.time classes built into Java can do this. The LocalTime
class represents a time-of-day without a date and without a time zone. This class includes the concept of second-of-day, how many seconds from the start of the day.
LocalTime lt = LocalTime.ofSecondOfDay( 3_754L );
But the result is not close to what you showed in your Question.
lt.toString(): 01:02:34
Do not use a time-of-day to represent elapsed time. Confusing and ambiguous. Don’t use a time-of-day class and don’t use a time-of-day style of string formatting.
For elapsed time in the range of day-hours-minutes-seconds, use the Duration
class.
Duration d = Duration.ofSeconds( 3_754L );
d.toString(): PT1H2M34S
That output is a String generated in the standard ISO 8601 format of PnYnMnDTnHnMnS
where the P
marks the beginning and the T
separates the years-month-days portion from the hours-minutes-seconds portion. So the result seen above means “one hour, two minutes, and thirty-four seconds”.
The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date
, .Calendar
, & java.text.SimpleDateFormat
.
The Joda-Time project, now in maintenance mode, advises migration to java.time.
To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.
Where to obtain the java.time classes?
The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval
, YearWeek
, YearQuarter
, and more.
Upvotes: 1
Reputation: 44061
In my own library Time4J (v1.2) a pattern-based solution ready for Java 6 and later looks like:
Duration<?> dur =
Duration.of(37540, ClockUnit.SECONDS).with(Duration.STD_CLOCK_PERIOD);
String s = Duration.Formatter.ofPattern("hh:mm:ss").format(dur);
System.out.println(s); // 10:25:40
In Joda-Time following code is possible using a builder approach:
PeriodFormatter f =
new PeriodFormatterBuilder().appendHours().appendLiteral(":").appendMinutes()
.appendLiteral(":").appendSeconds().toFormatter();
System.out.println("Joda-Time: " + f.print(new Period(37540 * 1000))); // 10:25:40
My previous posted solution was a field-based-workaround (using the field SECOND_OF_DAY) which has a serious disadvantage, namely to be limited to seconds less than 86400 (day-length). The accepted answer using old Calendar-code suffers from this bug, too, so it is not a real solution. In Java-8 (containing a new time library - JSR-310) there is also no solution available because it still misses the possibility to format durations. Sample outputs of the different proposals:
input = 337540 seconds (almost 4 days)
code of accepted solution => 21:45:40 (WRONG!!!)
Time4J-v1.2 => 93:45:40
Joda-Time => 93:45:40
Conclusion, use an external library for solving your problem.
Upvotes: 1
Reputation: 5291
Using java.util.Calendar:
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, 0);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.MILLISECOND, 0);
calendar.set(Calendar.SECOND, 37540);
System.out.println(new SimpleDateFormat("HH:mm:ss").format(calendar.getTime()));
Upvotes: 11