justdeko
justdeko

Reputation: 1124

Timepicker Integer Error

I have a timepicker in my preference activity for setting the time when a notification should be displayed. The value is stored as a string, for example: "15:45". To understand the problem, I will further explain what happens next to the value:

SharedPreferences pref= PreferenceManager.getDefaultSharedPreferences(context);
    String hour = pref.getString("notification_time","");
    // notification_time is my preference key
    String Hora = hour;
    int hours = Integer.parseInt(Hora.substring(0, 2));
    int min = Integer.parseInt(Hora.substring(3, 5));
    // as you can see, I parse the string, and then use the integers to set the time (see below)
    calendar.set(Calendar.HOUR_OF_DAY, hours);
    calendar.set(Calendar.MINUTE, min);
    calendar.set(Calendar.SECOND, 00);

Now the problem is, My TimePicker stores the value differently, if the time is AM: for example, if you set the time to 07:45, it stores the time in the string as "7:45", not "07:45", and thus this line in the code fails:

int hours = Integer.parseInt(Hora.substring(0, 2));

(Throwing this error, not really necessary to understand the problem):

java.lang.NumberFormatException: Invalid int: "5:"

,because the position for "substring" isnt working anymore. (1 digit stored in the string instead of 2). Same goes for minutes, for example if I set the minutes to 08, my timepicker stores them as 8, and the same problem occurs again.

Now I have thought about two ways to solve this problem: Either I change the code in my settingsactivity and parse the string differently, or I change the way how I store the strings:

if (positiveResult) {
        lastHour=picker.getCurrentHour();
        lastMinute=picker.getCurrentMinute();
        String time=String.valueOf(lastHour)+":"+String.valueOf(lastMinute);

        if (callChangeListener(time)) {
            persistString(time);
        }
        setSummary(getSummary());
    }

(These are the lines of code responsible for saving the value as a string)

How should I solve the problem?

Upvotes: 1

Views: 109

Answers (1)

Ryan J
Ryan J

Reputation: 8323

You could try using DateFormat's parse method to parse your notification time string. This will return a Date object that you can use to set the date/time of your Calendar object.

Something like:

DateFormat df = new SimpleDateFormat("H:mm"); // construct date formatter to recognize your time pattern
Date myDate = df.parse(pref.getString("notification_time",""));
calendar.setTime(myDate);  // set the time of calendar object

// do other stuff with date here...

This way, you don't need to bother with parsing the time and you can let existing tools do the work for you.

Simple test:

public class Test {
    public static void main(String[] args) throws Exception {
        DateFormat df = new SimpleDateFormat("H:mm");
        Date myDate = df.parse("17:45");
        System.out.println(myDate.toString());

        Calendar c = Calendar.getInstance();
        c.setTime(myDate);

        int hours = c.get(Calendar.HOUR_OF_DAY);
        int min = c.get(Calendar.MINUTE);

        System.out.println("Hour = " + hours + "\nMin = " + min);
    }
}

Produces:

Thu Jan 01 17:45:00 PST 1970
Hour = 17
Min = 45

Upvotes: 1

Related Questions