0

How to convert a number into hh:mm:ss format in apex code??

example : Integer time = 120; // in seconds

0

2 Answers 2

7

You can use DateTime to do this:

Integer seconds = 120;
System.debug(DateTime.valueOf(seconds*1000).formatGMT('HH:mm:ss'));

Output:

00:02:00

This works for values less than 24 hours, at which point it would wrap around.

2
  • Why K and not H? Mar 7, 2017 at 9:26
  • @FernandoGavinho Actually, you're right. I did mean HH.
    – sfdcfox
    Mar 7, 2017 at 9:28
0
Integer times = 120;
dateTime dt = DateTime.newInstance(0);
system.debug(dt.addseconds(times).formatGMT('HH:mm:ss'));

 // Output :  |DEBUG|00:02:00
1
  • It will be nice if you add sample output you got.
    – Reshma
    Mar 7, 2017 at 10:19

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .