1

I'm writing an app where I need to get the user set time from the start time time picker, and a user set time from an end time time picker. Then compare the start time and the end time with the current time. If the current time equals the start time the silence the phone. If the end time equals the current time un-silence the phone. Any help, especially code examples would be greatly appreciated. Thanks ahead of time.

public TimePickerDialog.OnTimeSetListener sTimeSetListener = new TimePickerDialog.OnTimeSetListener() {
        public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
            sHour = hourOfDay;
            sMinute = minute;
            updateDisplay();
            displayToast();

        }
    };

    public TimePickerDialog.OnTimeSetListener eTimeSetListener = new TimePickerDialog.OnTimeSetListener() {
        public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
            eHour = hourOfDay;
            eMinute = minute;
            updateDisplayE();
            displayToastE();

        }
    };

    public void updateDisplay() {
        startTime.setText(new StringBuilder().append(pad(sHour)).append(":")
                .append(pad(sMinute)));
        final int compareStartHour = sHour;
        Log.e("beginning", "Value " + sHour);
    }

    public void updateDisplayE()
 {
        endTime.setText(new StringBuilder().append(pad(eHour)).append(":")
                .append(pad(eMinute)));
    }

    private void displayToast() {
        Toast.makeText(
                this,
                new StringBuilder().append("Time choosen is ").append(
                        startTime.getText()), Toast.LENGTH_SHORT).show();

    }

    private void displayToastE() {
        Toast.makeText(
                this,
                new StringBuilder().append("Time choosen is ").append(
                        endTime.getText()), Toast.LENGTH_SHORT).show();

    }

    private static String pad(int c) {
        if (c >= 10)
            return String.valueOf(c);
        else
            return "0" + String.valueOf(c);
    }

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.preset_edit);
        save = (Button) findViewById(R.id.btnSave);
        TextView eventTxt = (TextView) findViewById(R.id.txtViewEvent);
        startTime = (Button) findViewById(R.id.btnStartTime);
        endTime = (Button) findViewById(R.id.btnEndTime);

        startTime.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                showDialog(TIME_DIALOG_ID);
            }
        });

        endTime.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                showDialog(TIME_DIALOG_ID2);
            }
        });
user2247767
  • 21
  • 2
  • 5

3 Answers3

4

Use Calendar class instance:

Calendar calendar=Calendar.getInstance();
calendar.set(Calendar.HOUR, tPicker.getCurrentHour());
calendar.set(Calendar.MINUTE, tPicker.getCurrentMinute());
calendar.set(Calendar.SECOND, tPicker.getCurrentSecond());
//you can also set the day, month, year etc., default values are today's

and then call

calendar.getTimeInMillis()

which will return you the time value in milliseconds (long), which you can easily compare to another.

For details, refer to the Calendar class documentation.

Andrii Chernenko
  • 9,873
  • 7
  • 71
  • 89
1

Get time from TimePicker and save it:

Time selectedTime = new Time(sHour, sMinute, 0);

Compare it with current time or any other time:

Date time = MyClassName.convertStringToTime(selectedTime.toString());

And pass both date values to a function:

public static int compareTime(Date date1, Date date2) {
        int result = date1.compareTo(date2);

        if (result > 0) //
        {
            return 1;
        } else if (result == 0) {
            return 0;
        }
        return -1;

    }

Edit: public static class MyClassName{ public static Date convertStringToTime(String strTime) { System.out.println("Time to be converted:"+strTime+"."); Date dateTobeReturned = null;

            DateFormat sdf = new SimpleDateFormat(DateUtility.TIME_FORMAT);

            try {
                dateTobeReturned = sdf.parse(strTime);
            } catch (ParseException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            System.out.println("Time: " + sdf.format(dateTobeReturned));


            return dateTobeReturned;
        }
    }

Please ask if you have any doubt. Hope it helps.

MysticMagicϡ
  • 28,593
  • 16
  • 73
  • 124
1

You can also try this..It works for me...First take a time from TimePickerDialog and get it into String then apply following code..

SimpleDateFormat sdf = new SimpleDateFormat("hh:mm");
            java.util.Date d_min = null;
            java.util.Date d_max = null;
            try {
                d_min = sdf.parse(time_min);//time_min is the Minimum time in String
                d_max = sdf.parse(time_max);time_max is the Maximun time in String
                d_compare = d_max.compareTo(d_min);
            } catch (ParseException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
 if (d_compare < 0) {
                check = 3;
                showDialog(0);
            } 

You can compare current time like this..Hope this will help you.. You can also take a look at this...current time

And this...link

Community
  • 1
  • 1
AndiM
  • 2,196
  • 2
  • 21
  • 38
  • I definitely will, I'm going down the list trying all the suggestions, if they work, I will accept. Thanks again! @Meghs – user2247767 Apr 12 '13 at 07:40
  • You said this works for you, so did you constantly get a syntax error right after java.util.Date d_max = null; ? Wanting you to add another {? @Meghs – user2247767 Apr 12 '13 at 08:01
  • No it doesn't give any syntax error.Work fine in my case.If you get an error then try to use Date object. – AndiM Apr 12 '13 at 10:21