Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Date Picker Dialog and Time Picker Dialog in Android

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 357
    Comment on it

    Android provides Date and Time Picker dialog, user can select appropriate date and time as per his need.

    With DatePickerDialog we can select date, month and year using Calendar class. setMaxDate(long maxDate) and setMinDate(long minDate) methods supports maximal and minimal date. DatePickerDialog.OnDateSetListener interface to receive a callback when the user sets the date.

    btnDate.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    final Calendar c = Calendar.getInstance();
                    year = c.get(Calendar.YEAR);
                    month = c.get(Calendar.MONTH);
                    day = c.get(Calendar.DAY_OF_MONTH);
    
                    DatePickerDialog dpd = new DatePickerDialog(CreateEventActivity.this,
                            new DatePickerDialog.OnDateSetListener() {
    
                                @Override
                                public void onDateSet(DatePicker view, int year,
                                                      int monthOfYear, int dayOfMonth) {
                                    c.set(Calendar.YEAR, year);
                                    c.set(Calendar.MONTH, month);
                                    c.set(Calendar.DAY_OF_MONTH, day);
    
                                    String format = new SimpleDateFormat("E, MMM d, yyyy").format(c.getTime());
    
                                    tvDate.setText(format);
    
                                }
                            }, year, month, day);
                    dpd.show();
                }
            });
    

    Similarly, hour and minute will be selected using TimePickerDialog. Implement the TimePickerDialog.OnTimeSetListener interface to receive a callback when the user sets the time.

    btnTime.setOnClickListener(new View.OnClickListener() {
    
            @Override
            public void onClick(View v) {
                final Calendar c = Calendar.getInstance();
                hour = c.get(Calendar.HOUR_OF_DAY);
                min = c.get(Calendar.MINUTE);
    
                TimePickerDialog tpd = new TimePickerDialog(CreateEventActivity.this,
                        new TimePickerDialog.OnTimeSetListener() {
    
                            @Override
                            public void onTimeSet(TimePicker view, int hourOfDay,
                                                  int minute) {
                                // Calling a getTime function for hour and minute of the day according to the 0-12 formate with AM, PM meridiem
    
                                String timeString = getTime(minute, hourOfDay);
                                if (timeString != null) {
                                    tvTime.setText(timeString);
                                } else {
                                    // Please select time
                                    Toast.makeText(getApplicationContext(), getResources().getString(R.string.time_msg), Toast.LENGTH_LONG).show();
                                }
    
                            }
                        }, hour, min, false);
                tpd.show();
            }
        });
    

    To Get Hour and Minute in a proper formate as AM and PM.

    public String getTime(int min, int hour) {
        String timeSet = null;
    
        if (hour > 12) {
            hour = hour-12;
            timeSet = "PM";
        } else if (hour == 0) {
            hour = hour+12;
            timeSet = "AM";
        } else if (hour == 12)
            timeSet = "PM";
        else
            timeSet = "AM";
    
        String Minute = null;
    
        if (min < 10)
            Minute = "0" + min;
        else
            Minute = String.valueOf(min);
    
        String aTime = new StringBuilder().append(hour).append(':')
                .append(min).append(" ").append(timeSet).toString();
        return aTime;
    }
    

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: