简体   繁体   中英

Return the time(int) in a String method

So I have this method that returns the time when a tv-show ends(eg: 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;
    }

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

If your getFHour() ; is returning a primitive type different than string you can do:

 msg1="" + getFHour();

similar with the other line:

msg2=""+ getFMin();

尝试使用日期格式化程序格式化电视节目开始的时间。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM