time problem

i am getting the time using java's Calendar

i have a problem on the minute part though

when the time is 9:04, it displays 9:4 instead and so on. please help on how i can display the proper minute? this is my code.

publicint getMinute()

{

return calendar.get(Calendar.MINUTE);

}

thanks

[489 byte] By [shuinia] at [2007-11-26 18:14:16]
# 1
Show us the code that displays the time. How are you formatting it, and why are you grabbing minutes on their own?Also have a look at SimpleDateFormat
TimRyanNZa at 2007-7-9 5:47:36 > top of Java-index,Java Essentials,Java Programming...
# 2
It appears that you're combing strings without doing the necessary formatting brfore combining them.If that's the case, you'll need to format the minutes value as a two-character string first.Or use Simple Date Format to format the time - this is the correct way to do it.
ChuckBinga at 2007-7-9 5:47:36 > top of Java-index,Java Essentials,Java Programming...
# 3

well, this is what i have so far

public int getHour()

{

return calendar.get(Calendar.HOUR_OF_DAY);

}

public int getMinute()

{

return calendar.get(Calendar.MINUTE);

}

public String getTime()

{

return getHour() + ":" + getMinute();

}

so when i do the getTime, it gives me 9:1, 9:2, instead of 9:01 and 9:02

shuinia at 2007-7-9 5:47:36 > top of Java-index,Java Essentials,Java Programming...
# 4

public int getHour()

{

return calendar.get(Calendar.HOUR_OF_DAY);

}

public int getMinute()

{

return calendar.get(Calendar.MINUTE);

}

public String getTime()

{

return getHour() + ":" + ((getMinute()<10)?("0"+getMinute()):(getMinute()));

}

Is a convenient but horribly bad way to fix your problem. As the other guys said, SimpleDateFormat is the answer :-)

sd4139a at 2007-7-9 5:47:36 > top of Java-index,Java Essentials,Java Programming...
# 5
lol.thanksi will also look into Simple date format
shuinia at 2007-7-9 5:47:36 > top of Java-index,Java Essentials,Java Programming...
# 6
SimpleDateFormat format = new SimpleDateFormat("hh:mm");String time = format(calendar.getTime());
TimRyanNZa at 2007-7-9 5:47:36 > top of Java-index,Java Essentials,Java Programming...
# 7
thank you
shuinia at 2007-7-9 5:47:36 > top of Java-index,Java Essentials,Java Programming...