0

Sorry for my english. I need to set time between 00:00:00 and 06:00:00, but my code doesn't work. I'm using joda library. My below method doesn't work, i can set time 14:00 or another, not not beetwen 00:00 and 6:00. What i'm doing wrong?

public void setTime(final TextView setText) {
        String dateStart = "00:00:00";
        String dateStop = "06:00:00";
        SimpleDateFormat format = new SimpleDateFormat("HH:mm:ss");

        Date d1 = null;
        Date d2 = null;

        try {
            d1 = format.parse(dateStart);
            d2 = format.parse(dateStop);

            DateTime dt1 = new DateTime(d1);
            DateTime dt2 = new DateTime(d2);

            int mHour = Hours.hoursBetween(dt1, dt2).getHours() % 24;
            int mMinute = Minutes.minutesBetween(dt1, dt2).getMinutes() % 60;

            TimePickerDialog tpd = new TimePickerDialog(context,
                    new TimePickerDialog.OnTimeSetListener() {

                        @Override
                        public void onTimeSet(TimePicker view, int hourOfDay,
                                              int minute) {

                            setText.setText(String.valueOf(hourOfDay) + ":" + String.valueOf(minute));
                        }

                    }, mHour, mMinute, true);
            tpd.show();
        }catch(Exception e) {
            Toast.makeText(getApplicationContext(), "Cant open time, sorry", Toast.LENGTH_SHORT).show();
            Log.e("Time error", e.toString());
        }
    }
Pier Giorgio Misley
  • 5,305
  • 4
  • 27
  • 66
g8214435
  • 737
  • 2
  • 6
  • 19

1 Answers1

0

Have a look here, with mHour and mMinute, you are removing from the DatePicker the hours before 6:00:00; copying from the link here is the problem:

public TimePickerDialog (Context context, int themeResId, 
TimePickerDialog.OnTimeSetListener listener, int hourOfDay, int
minute, boolean is24HourView)

Added in API level 1 Creates a new time picker dialog with the specified theme.

Parameters:

  • context the parent context
  • themeResId the resource ID of the theme to apply to this dialog
  • listener the listener to call when the time is set
  • hourOfDay the initial hour
  • minute the initial minute
  • is24HourView Whether this is a 24 hour view, or AM/PM.

here it is, leave them t 0 and 0 if you want all hours and minutes in your picker

EDIT:

if you want to set time only between 0 and 6, look at this post

Community
  • 1
  • 1
Pier Giorgio Misley
  • 5,305
  • 4
  • 27
  • 66