简体   繁体   English

在String方法中返回time(int)

[英]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. 所以我有这个方法返回电视节目结束的时间(例如:21:15),但是当小时或分钟小于10时,我需要方法返回这样的东西:21:05。

I tried like this, but because msg1 and msg2 are type String they can't return my int, if the elses occur. 我试过这样,但是因为msg1和msg2是String类型,所以如果elses发生,它们就不能返回我的int。

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. 您不能将int基元分配给字符串。 assuming your getFHour() and getFMin() returns int. 假设你的getFHour()和getFMin()返回int。 try: 尝试:

            msg1+=getFHour();  

             msg2+=getFMin;    

OR, you can also convert an int to a string with Integer.toString() method. 或者,您也可以使用Integer.toString()方法将int转换为字符串。

           msg1=Integer.toString(getFHour());  

             msg2=Integer.toString(getFMin);  

However you should be using SimpledateFormater to format Dates and Time 但是,您应该使用SimpledateFormater来格式化日期和时间

If your getFHour() ; 如果你的getFHour() ; is returning a primitive type different than string you can do: 返回一个不同于字符串的原始类型:

 msg1="" + getFHour();

similar with the other line: 与其他行类似:

msg2=""+ getFMin();

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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