There is a time variable, it stores seconds that need to be converted to the format HH: mm. The problem is that when using SimpleDateFormat or manual translation, the result is 00:00, even if 10 minutes have passed. Help pliz.

With this code example displays only seconds. (1 minute 3 seconds passed, displays "00:03" instead of "01:03")

DateFormat format = new SimpleDateFormat("mm:ss") ; format.setTimeZone(TimeZone.getTimeZone("UTC")); result= format.format(new Date(time*1000)); 
  • one
    Show the code ... - katso
  • Try resetting the time zone with the formatter format.setTimeZone(TimeZone.getTimeZone("UTC")); - Alex Chermenin
  • @katso Added code. - Melown
  • @AlexChermenin did not help - Melown

1 answer 1

 private static String timeToString(long secs) { long hour = secs / 3600, min = secs / 60 % 60, sec = secs / 1 % 60; return String.format("%02d:%02d:%02d", hour, min, sec); } 

Conclusion

 System.out.println(timeToString(63)); 

0:01:03

  • after 1 min 7 sec issued 0:00:07: ((( - Melown
  • Are you sure that there was the number 67 (ie, 1 minute and 7 seconds)? System.out.println("Secs:" + time + ". Formatted: " + timeToString(time)); - Chubatiy
  • Apparently you have your variable reset to zero after a minute (ie, after 60 seconds it becomes = 0 again) - Chubatiy
  • Yes exactly. But I have already solved the problem, only the translation in milliseconds and from them into the format helped. - Melown
  • I did not quite understand how this is connected with zeroing, but it is good what happened. Congratulations! - Chubatiy