For example I want convert 283392s to 3d 6h 43m 12s. Can this be achieved with SimpleDateFormat ?
Asked
Active
Viewed 692 times
-1
-
3this can be achieved by simple logic – Kaushal28 Aug 06 '16 at 09:15
-
Yes you can. Check the documents or search for it. http://stackoverflow.com/questions/25458832/how-can-i-convert-an-integer-e-g-19000101-to-java-util-date – Luna Aug 06 '16 at 09:16
-
Is 2833.... in seconds? – Přemysl Šťastný Aug 06 '16 at 09:16
-
I have already achieved this using maths but i am looking for some simple solution. – Ovais Chaudhry Aug 06 '16 at 09:18
-
@PřemyslŠťastný yes its in seconds. – Ovais Chaudhry Aug 06 '16 at 09:24
-
No you should not do it with `SimpleDateFormat` or any other formatter which is designed to format points in time. What you need is a formatter capable of formatting ELAPSED time. Either you study external library solutions (like in Joda-Time or my lib Time4J), or you apply your own work-around (see TimeUnit-answer below). – Meno Hochschild Aug 06 '16 at 15:02
2 Answers
0
In java 8 you can do the following one-liner:
import java.time.LocalTime;
System.out.println(LocalTime.MIN.plusSeconds(283392).toString());
That returns "6:43:12"

Dan Bmd
- 35
- 9
0
It should easily be done using TimeUnit like so:
int day = (int)TimeUnit.SECONDS.toDays(seconds);
long hours = TimeUnit.SECONDS.toHours(seconds) - (day *24);
long minute = TimeUnit.SECONDS.toMinutes(seconds) - (TimeUnit.SECONDS.toHours(seconds)* 60);
long second = TimeUnit.SECONDS.toSeconds(seconds) - (TimeUnit.SECONDS.toMinutes(seconds) *60);

David Landup
- 169
- 1
- 16