954,518 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

String format

Hello all,

I am trying to display time in the following format that represents hours and minutes:
hh:mm

public String getTime()
      {
        String time = String.format("%tI:%tM", hours, minutes);     
      
       return time;
      }


For some reason I keep getting the following error:
"The method format(String, Object[]) in the type String is not applicable for the arguments (String,
int, int)". Both time and minutes are integers. I am not sure what the problem could be. Any assistance will be appreciated.

Thanks Kim

KimJack
Junior Poster
114 posts since Apr 2006
Reputation Points: 8
Solved Threads: 0
 

You are using a format string that applies to a Calendar instance with int parameters. If you already have the hours and minutes as int, use String.format("%d:%d", hours, minutes); instead.

Ezzaral
Posting Genius
Moderator
15,986 posts since May 2007
Reputation Points: 3,250
Solved Threads: 847
 

Thanks, I tried that but still get the same error. Any other suggestions?

KimJack
Junior Poster
114 posts since Apr 2006
Reputation Points: 8
Solved Threads: 0
 

You can try using SimpleDateFormat, it could mean a few more lines of code though.

Regards,
Rohit

grohit
Newbie Poster
5 posts since Jun 2009
Reputation Points: 10
Solved Threads: 0
 

What is the datatype of hours, minutes variables?

As per java documentation the syntax of String.format is ...

public static String format(String format,Object... args)

If hours and minutes are int type,

String.format("%d:%d", hours, minutes); 
       // or
   String.format("%d:%d", new Object[]{hours, minutes});
__avd
Posting Genius (adatapost)
Moderator
8,648 posts since Oct 2008
Reputation Points: 2,136
Solved Threads: 1,241
 

HI
I am new to vc++. I created a simple window, want to dispaly a manu bar in that window. Kindly plz the Detail steps to get menu bar in my window.

Motiranjan
Newbie Poster
5 posts since May 2009
Reputation Points: 10
Solved Threads: 0
 

Motiranjan>I am new to vc++.
This is JAVA forum. Post your problem in C++ forum.

__avd
Posting Genius (adatapost)
Moderator
8,648 posts since Oct 2008
Reputation Points: 2,136
Solved Threads: 1,241
 

There must be some sort of error in Eclipse, because it is the only one that gives this error. However, I moved over to JGrasp and it works fine with your solution.

Is there any way to add a 0 for the minutes to keep the time formatted properly?

For example: if minutes are less than 10 it should display 12:08 instead of 12:8

Thanks

KimJack
Junior Poster
114 posts since Apr 2006
Reputation Points: 8
Solved Threads: 0
 
String time = String.format("%d:%02d", hours, minutes);
Ezzaral
Posting Genius
Moderator
15,986 posts since May 2007
Reputation Points: 3,250
Solved Threads: 847
 

Thanks all, it works fine, but I am curious as to why eclipse gives the error but not JGrasp.

KimJack
Junior Poster
114 posts since Apr 2006
Reputation Points: 8
Solved Threads: 0
 

Simple Solution, although this has been already solved:

String time = String.format("%d:%02d", Integer.toString(hours), Integer.toString( minutes) );


~ Xhamolk_

Xhamolk_
Newbie Poster
10 posts since Jun 2009
Reputation Points: 10
Solved Threads: 2
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You