简体   繁体   中英

Change from hour minute second to second from string

Now,I have a string String time= "200hour 0minute 0second" I want to track numbers in string and convert to second..As above string..I want to get value 200*3600.. My code:

    //change from hour minute to second
public String changeSecond(String time)
{
    String result;
    int ind_hour=time.indexOf("hour");
    int ind_minute=time.indexOf("minute");
    int ind_second=time.indexOf("second");

    int hour=Integer.parseInt(time.substring(0, ind_hour));
    int minute=Integer.parseInt(time.substring(ind_hour+1, ind_minute));;
    int second=Integer.parseInt(time.substring(ind_minute+1, ind_second));

    result=String.valueOf(hour*3600+minute*60+second);

    return result;
}

but when i run changeSecond(time).It don't work..How must I do.

public String changeSecond(String time)
{
    String result;
int ind_hour=time.indexOf("hour");
int ind_minute=time.indexOf("minute");
int ind_second=time.indexOf("second");

int hour=Integer.parseInt(time.substring(0, ind_hour));
int minute=Integer.parseInt(time.substring(ind_hour+6, ind_minute));; // value 6 is length of minute 
int second=Integer.parseInt(time.substring(ind_minute+6, ind_second));

  int totalsec=((hour*60*60)+(minute*60)+second);
   result=String.valueOf(totalsec);

return result;

}

Its not the way to do it.But It will work.Try this,

String hour="200hour 0minute 0second";

     String[] secondarray=hour.split("hour");

     String second=secondarray[0]; //here "second" will have the value 200

You can split the tab into 3 parts : One will contains the hours, another the minutes and the last one the seconds.

public static String changeSecond(String time)
    {
        String tab[] = time.split(" ");
        String result = "";
        int ind_hour=tab[0].indexOf("hour");
        int ind_minute=tab[1].indexOf("minute");
        int ind_second=tab[2].indexOf("second");

        int hour=Integer.parseInt(tab[0].substring(0, ind_hour));
        int minute=Integer.parseInt(tab[1].substring(0, ind_minute));
        int second=Integer.parseInt(tab[2].substring(0, ind_second));

        result=String.valueOf(hour*3600+minute*60+second);

        return result;
    }

You should make changes to the following lines:

int hour=Integer.parseInt(time.substring(0, ind_hour));
int minute=Integer.parseInt(time.substring(ind_hour+5, ind_minute));
int second=Integer.parseInt(time.substring(ind_minute+7, ind_second));

This is the cleanest solution I could come up with. Just split on " " then remove the text that's irrelevant.

public String changeSecond(final String time)
{
    final String[] times = time.split(" ");
    final int hour = Integer.parseInt(times[0].replace("hour", ""));
    final int minute = Integer.parseInt(times[1].replace("minute", ""));
    final int second = Integer.parseInt(times[2].replace("second", ""));
    return String.valueOf(hour*3600+minute*60+second);
}

The initial code was throwing parsing errors since alpha characters were being passed to the Integer.parseInt method. The alpha characters were being included because the substring did not take into account the length of the terms hour, minute and second. I would recommend splitting the string to tidy things up.

public String changeSecond(String time) {
    String[] tokens = time.split(" ");
    tokens[0] = tokens[0].substring(0, tokens[0].indexOf("hour"));
    tokens[1] = tokens[1].substring(0, tokens[1].indexOf("minute"));
    tokens[2] = tokens[2].substring(0, tokens[2].indexOf("second"));

    return String.valueOf(Integer.parseInt(tokens[0]) * 3600
            + Integer.parseInt(tokens[1]) * 60 + Integer.parseInt(tokens[2]));
}

Update your hours,minutes and seconds parsing as:

        int hour=Integer.parseInt(time.substring(0, ind_hour));
        int minute=Integer.parseInt(time.substring(ind_hour + "hour".length() + 1, ind_minute));
        int second=Integer.parseInt(time.substring(ind_minute + "minute".length() + 1, ind_second));

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