3

What is the proper way to turn an integer of seconds into a formatted string of hh:mm:ss in Java?

For instance:

int Seconds = 650
String Time = 00:10:50

Right now I'm using this:

String Time = new SimpleDateFormat("hh:mm:ss").format(new Date((Seconds*1000)));

But this seems to tack on hours for no reason, and I'm guessing it's because I'm misusing Date or SimpleDateFormat, but I'm too inexperienced to know what's wrong. Or is there just a built in system for this that I don't know about.

EDIT: I should point out that I know I could use simple division to peel out the hours, then the remaining minutes, then the remaining seconds, and patch all three of those pieces into a string, but I was wondering if Java has a baked-in way to do this.

fildred13
  • 2,280
  • 8
  • 28
  • 52

4 Answers4

4

You can use TimeUnit class defined in java.util.concurrent package.

for eg you want to calculate hours:

long hours=TimeUnit.SECONDS.toHours(seconds);

similar methods are available for calulating days, hours, minutes.

but mind you this will give you direct conversion to hours, so you will end up having more than 24 hours. For a proper implementation you need to first calculate the days, the do the necessary maths and the give the remaining value for calculating hours. Lastly write a string as per your required format.

User27854
  • 824
  • 1
  • 16
  • 40
2

There is actually a (good) reason "to tack on hours".

Date(long date) constructor's JavaDoc:

Allocates a Date object and initializes it to represent the specified number of milliseconds since the standard base time known as "the epoch", namely January 1, 1970, 00:00:00 GMT.

So, if your JVM is not running in the GMT timezone you're off accordingly.

It's by design and logical, too:

  • new Date() is expected to be your current local time
  • new Date(0) is expected to be January 1, 1970, 00:00:00 GMT + local offset = local time
  • new Date(650*1000) is expected to be January 1, 1970, 00:10:50 GMT + local offset = local time
Gerold Broser
  • 14,080
  • 5
  • 48
  • 107
1

your code is correct .but problem is this gives you the time relative to start position .think if you run following code

String Time = new SimpleDateFormat("EEEE,MMMM d,yyyy hh:mm,a").format(new Date((0)));
System.out.println(Time);
output>>Wednesday,December 31,1969 04:00,PM //this 04 will be reason to your undesired output later

i think that is the minimum time for positive seconds .think when you give milliseconds 0 java gives you day as Wednesday and 4 hours ,so starting hours of java time is not 0. so when you run following code

String Time = new SimpleDateFormat("hh:mm:ss").format(new Date((Seconds*1000)));

output>> 04:10:50 //you expect 00:10:50
because `starting time+seconds`
04:00:00 + 00:10:50 
but starting minutes and seconds are 0 so you only have problem about hours 

if you can subtract starting hours then you will get desired output. joda time library has interval so you can use it .take a look at this question

Community
  • 1
  • 1
Madhawa Priyashantha
  • 9,633
  • 7
  • 33
  • 60
1

Try it the following way,

    int seconds = 650;
    long millis = seconds * 1000;
    String format = String.format("%d:%d:%d",
            TimeUnit.MILLISECONDS.toHours(millis),
            TimeUnit.MILLISECONDS.toMinutes(millis),
            TimeUnit.MILLISECONDS.toSeconds(millis)
            - TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(millis))
    );
    System.out.println(format);
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
Muhammad
  • 6,725
  • 5
  • 47
  • 54