Giant
Giant

Reputation: 1649

time picker ontimeset is wrong

i have a date and time picker in my app and I've just noticed that the time is wrong. the current time that the time picker displays is i hour delay and its am/pm is wrong like for example the current time is 2:55 pm it will show 1:55 am what seems to be the problem here is the code

UPDATE:DATE AND TIME PICKER

@Override
public void onClick(View v) {

    if (v == btnCalendar) {

        // Process to get Current Date
        final Calendar c = Calendar.getInstance();
        mYear = c.get(Calendar.YEAR);
        mMonth = c.get(Calendar.MONTH);
        mDay = c.get(Calendar.DATE)+1;

        // Launch Date Picker Dialog
        DatePickerDialog dpd = new DatePickerDialog(this,
                new DatePickerDialog.OnDateSetListener() {

                    @Override
                    public void onDateSet(DatePicker view, int year,
                            int monthOfYear, int dayOfMonth) {
                        // Display Selected date in textbox
                        txtDate.setText(dayOfMonth + "-"
                                + (monthOfYear + 1) + "-" + year);

                    }
                }, mYear, mMonth, mDay);
        dpd.show();
    }
    if (v == btnTimePicker) {

        // Process to get Current Time
        final Calendar c = Calendar.getInstance();
        mHour = c.get(Calendar.HOUR)+1;
        mMinute = c.get(Calendar.MINUTE);

        // Launch Time Picker Dialog
        TimePickerDialog tpd = new TimePickerDialog(this,
                new TimePickerDialog.OnTimeSetListener() {

                    @Override
                    public void onTimeSet(TimePicker view, int hourOfDay,
                            int minute) {
                        // Display Selected time in textbox
                        txtTime.setText(hourOfDay + ":" + minute);
                    }
                }, mHour, mMinute, false);
        tpd.show();
    }
}
}

UPDATE: what i did is to put +1 in mHour = c.get(Calendar.HOUR_OF_DAY)+1; there might be a set back on this method any ideas?

unfixed is the AM/PM button is wrong it always shows am at start

UPDATE:AM/PM doesn't start with am at all is wrong because now that its morning it show pm but during pm it show am

UPDATE: there is also a problem in date.what i did is to put +1 in mDay = c.get(Calendar.DATE)+1; there might be a set back on this method any ideas?

Upvotes: 3

Views: 840

Answers (2)

Sagar Shah
Sagar Shah

Reputation: 4292

You can try something like this:

 <TimePicker
            android:id="@+id/timePicker"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/datePicker1"
            android:layout_centerHorizontal="true"
            android:layout_margin="20dp" />

TimePicker tp = (TimePicker)findViewById(R.id.timePicker);

 btn.setOnClickListener(new OnClickListener() 
 {

            @Override
            public void onClick(View v) 
            {
                // TODO Auto-generated method stub

                int Hour = tp.getCurrentHour();
                int Minute = tp.getCurrentMinute();

                    txtTime.setText(String.valueOf(Hour) + ":" + String.valueOf(Minute));

            }
        });

Upvotes: 0

Padma
Padma

Reputation: 656

Actually, Its a bug. It has been reported to google. You can find it here http://code.google.com/p/android/issues/detail?id=24388 and http://code.google.com/p/android/issues/detail?id=18982

Upvotes: 3

Related Questions