Search code examples
javaandroidtimedatepickertimepicker

Android TimePickerDialog - Only works half the time (Literally)


Mac OS-X

ECLIPSE with ADT

ANDROID

................

Problem: When I pick the time, say 06:00, it sets the time as 18:00, when I pick the time 18:00 it sets the time as 06:00. I can't seem to figure out why.

................

LOGIC

Gets current date instance.

OnClickListener is set to a ButtonImage.

OnClick calls showTimeDialog with 2 arguments (a TextView, the current date instance).

TimePicker Dialog shows with at a set time of 09:00.

TimeSet sets the time of a Calendar activeTime and updates a TextView

..............

ADDITIONAL NEW INFO

Ok, so strange thing happened, now my code seems to work, BUT last time I checked it was before 24:00 and it was acting as described. It works now and the time is 00:20 here in the UK. I dont know if this has anything to do with it but I will update this post if anything changes.

ADDITIONAL NEW INFO 2

My code seems to work between 00:00 - 12:00 but from 12:00 - 24:00 it doesn't. I'm stumped..

.............

Here is my code:

    /////// SETUP TIME

    //  capture our View elements for the start time function
    startTimeDisplay = (TextView) findViewById(R.id.editText2);
    endTimeDisplay = (TextView) findViewById(R.id.editText3);
    startPickTime = (ImageButton) findViewById(R.id.imageButtonStartTime);
    endPickTime = (ImageButton) findViewById(R.id.imageButtonEndTime);

    // Use the current time as the default values for the time picker
    startTime = Calendar.getInstance();
    endTime = Calendar.getInstance();
    startTime.get(Calendar.DATE);
    endTime.get(Calendar.DATE);

    // add a click listener to the button
    startPickTime.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            showTimeDialog(startTimeDisplay, startTime);
        }
    });

    /* add a click listener to the button   */
    endPickTime.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            showTimeDialog(endTimeDisplay, endTime);
        }
    });
}
@Override
protected Dialog onCreateDialog(int id) {

    getTime(activeTime);
    int hr = activeTime.get(Calendar.HOUR_OF_DAY);
    int mn = activeTime.get(Calendar.MINUTE);

    Log.d(TAG, "first hr..." +hr);
    Log.d(TAG, "first mn..." +mn);


    switch (id) {
        case DATE_DIALOG_ID:
            return new DatePickerDialog(this, dateSetListener, activeDate.get(Calendar.YEAR), activeDate.get(Calendar.MONTH), activeDate.get(Calendar.DAY_OF_MONTH));
        case TIME_DIALOG_ID:
            return new TimePickerDialog(this, timeSetListener, THE_HOUR, THE_MINUTE, true);
    }
    return null;
}

@Override
protected void onPrepareDialog(int id, Dialog dialog) {
    super.onPrepareDialog(id, dialog);

    getTime(activeTime);
    int hr = activeTime.get(Calendar.HOUR_OF_DAY);
    int mn = activeTime.get(Calendar.MINUTE);

    Log.d(TAG, "second hr..." +hr);
    Log.d(TAG, "second mn..." +mn);

    switch (id) {
        case DATE_DIALOG_ID:
            ((DatePickerDialog) dialog).updateDate(activeDate.get(Calendar.YEAR), activeDate.get(Calendar.MONTH), activeDate.get(Calendar.DAY_OF_MONTH));
            break;
        case TIME_DIALOG_ID:
            ((TimePickerDialog) dialog).updateTime(THE_HOUR, THE_MINUTE);
            break;
    }
}

public void showTimeDialog(TextView timeDisplay, Calendar date) {
    activeTimeDisplay = timeDisplay;
    activeTime = date;
    showDialog(TIME_DIALOG_ID);
}

private OnTimeSetListener timeSetListener = new OnTimeSetListener() {
    @Override
    public void onTimeSet(TimePicker view, int hour, int minute) {
        activeTime.set(Calendar.HOUR, hour);
        activeTime.set(Calendar.MINUTE, minute);
        updateTimeDisplay(activeTimeDisplay, activeTime);
        unregisterTimeDisplay();
    }
};

private void updateTimeDisplay(TextView timeDisplay, Calendar date) {

    //convert Calendar to Unix Time
    long activeTimeUnixTime = (long) (date.getTimeInMillis());

    //convert Unix Time to Date
    java.util.Date startTimeDate = new java.util.Date(activeTimeUnixTime);

    //convert Date to SimpleDateFormat and convert to String
    SimpleDateFormat formatter2;
    formatter2 = new SimpleDateFormat("kk:mm", Locale.UK);
    String stringTime = formatter2.format(startTimeDate);

    timeDisplay.setText(stringTime);
}

private void unregisterTimeDisplay() {
    activeTimeDisplay = null;
    activeTime = null;
}

Solution

  • SOLUTION:

    My onTimeSetListener onTimeSet() method was using HOUR instead of HOUR_OF_DAY.

    Okay so I was stuck on this for ages but finally figured it out. But because Im quite new to Android and Calendar the difference alluded me.

    Calendar.HOUR is 5 for 17:00 because it's the 5th hour in the PM. Calendar.HOUR_OF_DAY returns 17 for 17:00. So here is the correct code:

    private OnTimeSetListener timeSetListener = new OnTimeSetListener() {
        @Override
        public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
            activeTime.set(Calendar.HOUR_OF_DAY, hourOfDay);
            activeTime.set(Calendar.MINUTE, minute);
            updateTimeDisplay(activeTimeDisplay, activeTime);
            unregisterTimeDisplay();
        }
    };