Skip to content
Advertisement

How to convert input type [closed]

I am making timer when user set time and when I want to set time in second “mm:ss”(minutes:seconds) formula my app stops becouse app doesn’t accept colon. So how to convert input type from “mm:ss” to “mmss” that the textview image does not change but in the program changed to the format as I mentioned above

here is my code `

    mEditTextInput = (EditText) findViewById(R.id.editText);
    btn1 = (Button) findViewById(R.id.button);
    mButtonSet=findViewById(R.id.button_set);
    mTextViewCountDown = findViewById(R.id.textView);


    mButtonSet.setOnClickListener(new View.OnClickListener() {




        @Override
        public void onClick(View v) {
            String input = mEditTextInput.getText().toString();

           public long getTimeInLong(String input) {
                StringBuilder builder = new StringBuilder();
                String[] splittedString = input.split(":");
                builder.append(splittedString[0]);
                builder.append(splittedString[1]);

                return Long.parseLong(builder.toString());
            }



            setTime(millisInput);
            mEditTextInput.setText("%02d%02d");


        }
    });





    btn1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            mCountDownTimer=new CountDownTimer(mTimeLeftInMillis,1000) {
                @Override
                public void onTick(long millisUntilFinished) {
                    mTimeLeftInMillis=millisUntilFinished;
                    updateCountDownText();

                }

                @Override
                public void onFinish() {

                }
            }.start();
        }
    });

    updateCountDownText();


}

private void updateCountDownText(){
    int hours = (int) (mTimeLeftInMillis / 1000) / 3600;
    int minutes = (int) ((mTimeLeftInMillis / 1000) % 3600) / 60;
    int seconds = (int) (mTimeLeftInMillis / 1000) % 60;
    String timeLeftFormatted;

        timeLeftFormatted = String.format(Locale.getDefault(),
                "%02d:%02d", minutes, seconds);

    mTextViewCountDown.setText(timeLeftFormatted);

}
private void setTime(long milliseconds) {
    resetTimer();

    mStartTimeInMillis = milliseconds;

}
private void resetTimer() {
    mTimeLeftInMillis = mStartTimeInMillis;
    updateCountDownText();
}


public void decrease2Integer(View view) {
    if (czas2 >=5) {
        czas2 = czas2 - 5;
        display3(czas2);
    }

}


public void increase2Integer(View view) {
    czas2 = czas2 + 5;
    display3(czas2);
}
private void display3(int time2) {
    TextView display3Integer = (EditText) findViewById(R.id.editText);
    String formattedTime1 =String.format( "%02d:%02d", time2 / 60, time2 % 60);




    display3Integer.setText(formattedTime1);

}

LOGCAT

 Process: com.example.timertestyjeabcdisa, PID: 10071
java.lang.NumberFormatException: For input string: "00:15"
    at java.lang.Long.parseLong(Long.java:594)
    at java.lang.Long.valueOf(Long.java:808)
    at com.example.timertestyjeabcdisa.MainActivity$1.onClick(MainActivity.java:56)
    at android.view.View.performClick(View.java:7125)
    at android.view.View.performClickInternal(View.java:7102)
    at android.view.View.access$3500(View.java:801)
    at android.view.View$PerformClick.run(View.java:27336)
    at android.os.Handler.handleCallback(Handler.java:883)
    at android.os.Handler.dispatchMessage(Handler.java:100)
    at android.os.Looper.loop(Looper.java:214)
    at android.app.ActivityThread.main(ActivityThread.java:7356)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)

Advertisement

Answer

For the string to be parseable as long, it must not contain special characters like ":" . A function to convert the time containing “:” to long may look like this :

public long getMillisTimeInLong(String timeInMinsSecsWithColon) {
    String[] splittedString = timeWithColon.split(":");
    int min = Integer.parseInt(splittedString[0]);
    int sec = Integer.parseInt(splittedString[1]);
    return min*60*1000 + sec*1000;
}

EDIT: Taking @Joakim Danielson’s comment into consideration, the function is modified as above.

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement