Hello all,

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

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

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.

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

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


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});

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>I am new to vc++.
This is JAVA forum. Post your problem in C++ forum.

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


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

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

Simple Solution, although this has been already solved:

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

~ Xhamolk_