The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.

Time String to int seconds

Please help!

I am trying to convert a time string to an integer in seconds (eg. 1:25 = 85 secs) but cannot seem to figure out how best to go about doing this. I have so far used a delimiter to remove the ":" from the string and separate the minutes from the seconds. I also have a method to take minutes and seconds and add them to form only seconds. Both methods are separate but I would like to combine them somehow. Could someone please show me how this is possible. I may not even be on the right track and it is proving very frustrating. Thanks

Re: Time String to int seconds

What I'm trying to achieve is one method not two. I simply need it to convert the time string into int seconds ready for later manipulation. I am guessing I am going about this the wrong way but I cannot think of a better way to do it. sorry

Re: Time String to int seconds

Call me dumb lol. But I really wouldn't know how. I am just a beginner at this and am really drowning at the moment but am determined to grasp it somehow. Is there a working example anywhere that I can access to see how it works?

Re: Time String to int seconds

I don't know, say you have these two methods:

publicvoid doThing1(){//thing 1}publicvoid doThing2(){//thing 2}

...and then you are tasked with creating a single method that accomplishes both thing 1 and thing 2. Instead of copying and pasting both things into a new method, doesn't it make sense just to do this?

Re: Time String to int seconds

Ok I think I understand what your saying. I am clear on your theory. My time values are minutes and seconds extracted from strings within a file. What I am having difficulty in doing is the conversion from 3:25 as a string to 205 seconds as an integer. My simple question would be then how is this achieved. Do I still use two methods or can this be achieved in one. So String to int conversion (in seconds).