Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Number of seconds in "HH:MM:SS"

What's the best way to get the number of seconds in a string representation like "hh:mm:ss"?

Obviously Integer.parseInt(s.substring(...)) * 3600 + Integer.parseInt(s.substring(...)) * 60 + Integer.parseInt(s.substring(...)) works.

But I don't want to test that, and reinvent the wheal, I expect there is a way to use DateTimeFormat or other classes from standard libraries.

Thanks!

like image 796
Bogdan Balan Avatar asked Jul 08 '10 17:07

Bogdan Balan


People also ask

How do you convert seconds to HH mm SS?

To convert seconds to HH:MM:SS :Multiply the seconds by 1000 to get milliseconds. Pass the milliseconds to the Date() constructor. Use the toISOString() method on the Date object. Get the hh:mm:ss portion of the string.

How do I convert seconds to HH mm SS in Excel?

As with Excel, the first step to converting elapsed second to time is to divide the value by 86400. To format the cells for mm:ss, select Format > Number > More Formats > More date and time formats from the Menu.

How to convert the number of seconds to HH MMSS format?

To convert the number of seconds to hh:mm:ss format, please apply the below formula: =TEXT(A1/(24*60*60),"hh:mm:ss") Please try it, hope it can help you!

Is there a way to convert HH to seconds?

You might also like the online Convert HH:MM:SS to seconds tool . This tool converts a number of seconds in human readable time using hours/minutes/seconds.

Is there a tool to convert hours to seconds?

This tool converts human readable time using hours/minutes/seconds to seconds. You might also like the online Convert seconds to HH:MM:SS tool . This tool converts human readable time using hours/minutes/seconds to seconds.

How to convert a time in seconds to a string format?

Given a time in seconds and the task is to convert the time into a string format hh:mm:ss. There are two approaches to solve this problem: The Date () constructor expects a UNIX timestamp as one of its forms. A UNIX timestamp is a number of milliseconds that have passed since the epoch time (January 1, 1970, 00:00:00 UTC).


3 Answers

An original way: The Calendar version (updated with the suggestions in the comments):

DateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");
dateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
Date date = dateFormat.parse(string);
//Here you can do manually date.getHours()*3600+date.getMinutes*60+date.getSeconds();
//It's deprecated to use Date class though.
//Here it goes an original way to do it.
Calendar time = new GregorianCalendar();
time.setTime(date);
time.setTimeZone(TimeZone.getTimeZone("UTC"));
time.set(Calendar.YEAR,1970); //Epoc year
time.set(Calendar.MONTH,Calendar.JANUARY); //Epoc month
time.set(Calendar.DAY_OF_MONTH,1); //Epoc day of month
long seconds = time.getTimeInMillis()/1000L;

Disclaimer: I've done it by heart, just looking at the documentation, so maybe there is a typo or two.

like image 33
pakore Avatar answered Oct 21 '22 13:10

pakore


Based on pakores solution:

    DateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");
    Date reference = dateFormat.parse("00:00:00");
    Date date = dateFormat.parse(string);
    long seconds = (date.getTime() - reference.getTime()) / 1000L;

reference is used to compensate for different timezones and there is no problem with daylight saving time because SimpleDateFormat does NOT use the actual date, it return the Epoc date (January 1st, 1970 = no DST).

Simplifying (not much):

    DateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");
    dateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
    Date date = dateFormat.parse("01:00:10");
    long seconds = date.getTime() / 1000L;

but I would still have a look at Joda-Time...

like image 169
user85421 Avatar answered Oct 21 '22 13:10

user85421


joda-time is 1 options. infact i prefer that library for all date manipulations. I was going thru the java 5 javadoc and found this enum class which is simple and useful for you. java.util.concurrent.TimeUnit. look at the convert(...) methods. http://download.oracle.com/docs/cd/E17476_01/javase/1.5.0/docs/api/java/util/concurrent/TimeUnit.html

like image 1
Aravind Yarram Avatar answered Oct 21 '22 14:10

Aravind Yarram