String format

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Apr 2006
Posts: 114
Reputation: KimJack is an unknown quantity at this point 
Solved Threads: 0
KimJack KimJack is offline Offline
Junior Poster

String format

 
0
  #1
Jun 3rd, 2009
Hello all,

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

  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
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 4,483
Reputation: Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of 
Solved Threads: 515
Moderator
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is offline Offline
Industrious Poster

Re: String format

 
0
  #2
Jun 3rd, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 114
Reputation: KimJack is an unknown quantity at this point 
Solved Threads: 0
KimJack KimJack is offline Offline
Junior Poster

Re: String format

 
0
  #3
Jun 3rd, 2009
Thanks, I tried that but still get the same error. Any other suggestions?
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 5
Reputation: grohit is an unknown quantity at this point 
Solved Threads: 0
grohit grohit is offline Offline
Newbie Poster

Re: String format

 
0
  #4
Jun 3rd, 2009
You can try using SimpleDateFormat, it could mean a few more lines of code though.

Regards,
Rohit
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 2,639
Reputation: adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of 
Solved Threads: 472
Moderator
adatapost's Avatar
adatapost adatapost is offline Offline
Posting Maven

Re: String format

 
0
  #5
Jun 4th, 2009
What is the datatype of hours, minutes variables?

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

  1. public static String format(String format,Object... args)
If hours and minutes are int type,
  1. String.format("%d:%d", hours, minutes);
  2. // or
  3. String.format("%d:%d", new Object[]{hours, minutes});
Failure is not fatal, but failure to change might be. - John Wooden
Reply With Quote Quick reply to this message  
Join Date: May 2009
Posts: 5
Reputation: Motiranjan is an unknown quantity at this point 
Solved Threads: 0
Motiranjan Motiranjan is offline Offline
Newbie Poster

Re: String format

 
0
  #6
Jun 4th, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 2,639
Reputation: adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of 
Solved Threads: 472
Moderator
adatapost's Avatar
adatapost adatapost is offline Offline
Posting Maven

Re: String format

 
0
  #7
Jun 4th, 2009
Motiranjan>I am new to vc++.
This is JAVA forum. Post your problem in C++ forum.
Failure is not fatal, but failure to change might be. - John Wooden
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 114
Reputation: KimJack is an unknown quantity at this point 
Solved Threads: 0
KimJack KimJack is offline Offline
Junior Poster

Re: String format

 
0
  #8
Jun 4th, 2009
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
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 4,483
Reputation: Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of 
Solved Threads: 515
Moderator
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is offline Offline
Industrious Poster

Re: String format

 
0
  #9
Jun 4th, 2009
  1. String time = String.format("%d:%02d", hours, minutes);
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 114
Reputation: KimJack is an unknown quantity at this point 
Solved Threads: 0
KimJack KimJack is offline Offline
Junior Poster

Re: String format

 
0
  #10
Jun 6th, 2009
Thanks all, it works fine, but I am curious as to why eclipse gives the error but not JGrasp.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC