Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Return the time(int) in a String method

So I have this method that returns the time when a tv-show ends(e.g.: 21:15),but when the hour or minute is under 10 I need the method to return something like this : 21:05.

I tried like this, but because msg1 and msg2 are type String they can't return my int, if the elses occur.

What can I do? Helpfull Info: Im in my first year of Computer Engineering.

public static final int ZERO = 0;

 public String endsWhen(){
        String msg1="";
        String msg2="";

        if(fhour<10)
            msg1=ZERO + getFHour();
        else
            msg1=getFHour();  //error here

        if(fmin<10)
            msg2=ZERO+getFMin;
        else
            msg2=getFMin;    //error here

        return  msg1 + ":" + msg2;
    }
like image 456
JoMP Avatar asked Aug 10 '26 00:08

JoMP


1 Answers

You cannot assign an int primitive to a string. assuming your getFHour() and getFMin() returns int. try:

            msg1+=getFHour();  

             msg2+=getFMin;    

OR, you can also convert an int to a string with Integer.toString() method.

           msg1=Integer.toString(getFHour());  

             msg2=Integer.toString(getFMin);  

However you should be using SimpledateFormater to format Dates and Time

like image 174
PermGenError Avatar answered Aug 12 '26 14:08

PermGenError



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!