• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Cannot Convert String to Int. Clock question.

 
Christopher Schneider
Greenhorn
Posts: 11
1
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello folks, back again. The book I'm reading had me create a simple clock program, and it works fine.

However, when it displays the time, if the minutes are below 10, it doesn't show a zero in front, and it doesn't look right. e.g. if it's 10:09, it shows up at 10:9 in the window. It's not a big deal, and I'm still moving on to the next chapter of the book, but I'd like to know how to do this anyway. Here's the code (irrelevant information excluded):

 
Paul Clapham
Sheriff
Pie
Posts: 20171
25
MySQL Database
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Use a DecimalFormat object if you want to format a number in some specific way.
 
Campbell Ritchie
Sheriff
Pie
Posts: 47229
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Or go through the java.util.Formatter class to read about the % tags. Look through those after %T. They will probably allow you to dispense with the + 1 for month. They will take the Calendar object, so you don't need to separate it into separate fields.
 
Rob Spoor
Sheriff
Pie
Posts: 20372
44
Chrome Eclipse IDE Java Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Clapham wrote:Use a DecimalFormat object if you want to format a number in some specific way.

Or DateFormat (usually SimpleDateFormat) to format a Date object directly. That can take care of adding the month names as well; a Locale can be used to format them in a specific language. The Calendar can then be dropped in favour of a new Date() object.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic