How can i convert seconds to minutes and seconds?

suppose i have:int seconds = 101 anybody have any ideas how i can convert this to this format? 1:41thanks
[159 byte] By [kevin123a] at [2007-11-26 21:51:32]
# 1
i think i might have figured this out
kevin123a at 2007-7-10 3:45:00 > top of Java-index,Java Essentials,Java Programming...
# 2
What about using division and modulo operators ?
TimTheEnchantora at 2007-7-10 3:45:00 > top of Java-index,Java Essentials,Java Programming...
# 3
got itint length = songLength/60 + songLength%60;edit: this is wrong, i'll figure it outMessage was edited by: kevin123
kevin123a at 2007-7-10 3:45:00 > top of Java-index,Java Essentials,Java Programming...
# 4
> got it> > int length = songLength/60 + songLength%60;Now you're adding minutes to seconds: 1 minute plus 41 seconds is, in your case, 42. Which is not your intention, I guess.Try this:String s =
prometheuzza at 2007-7-10 3:45:00 > top of Java-index,Java Essentials,Java Programming...
# 5
how can i add a zero if i had 61 seconds:it shows up as 1:1it should be 1:01thanks for the help, i did it a much harder way than you gave me for some stupid reason.
kevin123a at 2007-7-10 3:45:00 > top of Java-index,Java Essentials,Java Programming...
# 6

String time = (songLength/60)+":"+ (songLength%60);

if (songLength%60 < 10)

time = (songLength/60)+":"+ "0" + (songLength%60);

return time;

i got it all right there, thanks for the help

kevin123a at 2007-7-10 3:45:01 > top of Java-index,Java Essentials,Java Programming...
# 7

String DATE_FORMAT = "dd/MM/yyyy HH:mm:ss"; //for example

// TODO Auto-generated method stub

Calendar calendar = Calendar.getInstance();

calendar.set(Calendar.SECOND, calendar.get(Calendar.MINUTE)+500);

System.out.println(calendar.get(Calendar.MINUTE));

Date customDate = calendar.getTime();

SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT);

//System.out.println(customDate);//you can format this date

System.out.println(sdf.format(customDate));

java_2006a at 2007-7-10 3:45:01 > top of Java-index,Java Essentials,Java Programming...
# 8

> >

>

> String time = (songLength/60)+":"+

> (songLength%60);

>if (songLength%60 < 10)

> time = (songLength/60)+":"+ "0" +

> (songLength%60);

>return time;

> got it all right there, thanks for the help

This can be simplified using a Java 1.5 feature

String formattedMinutes = String.format("%d:%02d", minutes / 60, minutes % 60);

sabre150a at 2007-7-10 3:45:01 > top of Java-index,Java Essentials,Java Programming...
# 9

> String time = (songLength/60)+":"+(songLength%60);

>if (songLength%60 < 10)

> time = (songLength/60)+":"+ "0" +(songLength%60);

>return time;

>

> got it all right there, thanks for the help

You can make that a one-liner:String format(int seconds) {

return (seconds/60)+((seconds%60)<10?":0":":")+(seconds%60);

}

Message was edited by:

prometheuzz

Or beter, like sabre150 did!

prometheuzza at 2007-7-10 3:45:01 > top of Java-index,Java Essentials,Java Programming...
# 10

> String DATE_FORMAT = "dd/MM/yyyy HH:mm:ss"; //for

> example

>

> // TODO Auto-generated method stub

> Calendar calendar = Calendar.getInstance();

>

> calendar.set(Calendar.SECOND,

> D, calendar.get(Calendar.MINUTE)+500);

> System.out.println(calendar.get(Calendar.MINUTE));

> Date customDate = calendar.getTime();

>

> SimpleDateFormat sdf = new

> ew SimpleDateFormat(DATE_FORMAT);

> //System.out.println(customDate);//you can format

> this date

> System.out.println(sdf.format(customDate));

I have a real problem with this approach. A number of minutes are not a date. They only become a date when relative to some point in time and your approach uses 1st Jan 1970. Since this approach uses SimpleDateFormat which have a built in time zone, the approach will fail for time differences that involve daylight saving and Leap years.

sabre150a at 2007-7-10 3:45:01 > top of Java-index,Java Essentials,Java Programming...