943,770 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 7855
  • Java RSS
You are currently viewing page 1 of this multi-page discussion thread
Jun 3rd, 2009
0

String format

Expand Post »
Hello all,

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

Java Syntax (Toggle Plain Text)
  1. public String getTime()
  2. {
  3. String time = String.format("%tI:%tM", hours, minutes);
  4.  
  5. return time;
  6. }

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
Reputation Points: 8
Solved Threads: 0
Junior Poster
KimJack is offline Offline
114 posts
since Apr 2006
Jun 3rd, 2009
0

Re: String format

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.
Moderator
Featured Poster
Reputation Points: 3239
Solved Threads: 838
Posting Genius
Ezzaral is offline Offline
6,761 posts
since May 2007
Jun 3rd, 2009
0

Re: String format

Thanks, I tried that but still get the same error. Any other suggestions?
Reputation Points: 8
Solved Threads: 0
Junior Poster
KimJack is offline Offline
114 posts
since Apr 2006
Jun 3rd, 2009
0

Re: String format

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

Regards,
Rohit
Reputation Points: 10
Solved Threads: 0
Newbie Poster
grohit is offline Offline
5 posts
since Jun 2009
Jun 4th, 2009
0

Re: String format

What is the datatype of hours, minutes variables?

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

Java Syntax (Toggle Plain Text)
  1. public static String format(String format,Object... args)
If hours and minutes are int type,
Java Syntax (Toggle Plain Text)
  1. String.format("%d:%d", hours, minutes);
  2. // or
  3. String.format("%d:%d", new Object[]{hours, minutes});
Moderator
Reputation Points: 2136
Solved Threads: 1228
Posting Genius
adatapost is offline Offline
6,527 posts
since Oct 2008
Jun 4th, 2009
0

Re: String format

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.
Last edited by Motiranjan; Jun 4th, 2009 at 7:38 am.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Motiranjan is offline Offline
5 posts
since May 2009
Jun 4th, 2009
0

Re: String format

Motiranjan>I am new to vc++.
This is JAVA forum. Post your problem in C++ forum.
Moderator
Reputation Points: 2136
Solved Threads: 1228
Posting Genius
adatapost is offline Offline
6,527 posts
since Oct 2008
Jun 4th, 2009
0

Re: String format

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
Reputation Points: 8
Solved Threads: 0
Junior Poster
KimJack is offline Offline
114 posts
since Apr 2006
Jun 4th, 2009
0

Re: String format

java Syntax (Toggle Plain Text)
  1. String time = String.format("%d:%02d", hours, minutes);
Moderator
Featured Poster
Reputation Points: 3239
Solved Threads: 838
Posting Genius
Ezzaral is offline Offline
6,761 posts
since May 2007
Jun 6th, 2009
0

Re: String format

Thanks all, it works fine, but I am curious as to why eclipse gives the error but not JGrasp.
Reputation Points: 8
Solved Threads: 0
Junior Poster
KimJack is offline Offline
114 posts
since Apr 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: How to login automatically in online accounts using java?
Next Thread in Java Forum Timeline: Help with Database errors





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC