SimpleDateFormat running on the clock for no reason?

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

For example:

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

      

I am currently using this:

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

      

But this seems to apply to the watch for no reason, and I guess it is because I am overusing Date

or SimpleDateFormat

, but I am too inexperienced to find out what happened. Or is there just a built-in system for this that I am not aware of.

EDIT: I should note that I know I can use a simple split to clear the hours, the remaining minutes, then the remaining seconds, and fix all three parts into a string, but I was wondering if Java has a baked way of doing this.

+3


source to share


4 answers


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

for example you want to calculate hours:

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

      



Similar methods are available for calculating days, hours, minutes.

but remember this will give you a direct conversion to hours, so you will get over 24 hours. For a correct implementation, you need to first calculate the days, perform the necessary math calculations, and give the remaining value to calculate the hours. Finally, write the string according to your required format.

+4


source


There is actually a (good) reason to "climb by the clock".

Date(long date)

JavaDoc constructor
:

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 GMT timezone, you are disconnected accordingly.

This is also by design and logic:

  • Expected that
  • new Date()

    will be your current local time.
  • new Date(0)

    expected January 1, 1970 00:00:00 GMT + local offset = local time
  • new Date(650*1000)

    expected January 1, 1970 00:10:50 GMT + local offset = local time
+2


source


your code is correct. but the problem is that it gives you the time relative to the starting position. If you use the 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 this is the minimum time for positive seconds. If you give milliseconds 0 java gives you wednesday day and 4 hours, so starting hours of java time is not 0.

  so when you run the 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 deduct the starting hours, then you will get the desired result. joda time library

has interval

, so you can use it. Look atthis question

+1


source


Try the following:

    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);

      

+1


source







All Articles