How to get date from date picker in android?

前端 未结 7 1566
眼角桃花
眼角桃花 2021-01-13 10:29

I am using the DatePicker for my application. I want to get the date that I have selected (on the DatePicker), but it\'s not returning the selected

相关标签:
7条回答
  • 2021-01-13 11:08
    private DatePickerDialog.OnDateSetListener datePickerListener 
                    = new DatePickerDialog.OnDateSetListener() {
    
            // when dialog box is closed, below method will be called.
            public void onDateSet(DatePicker view, int selectedYear,
                    int selectedMonth, int selectedDay) {
                year = selectedYear;
                month = selectedMonth;
                day = selectedDay;
    
                // set selected date into textview
                tvDisplayDate.setText(new StringBuilder().append(month + 1)
                   .append("-").append(day).append("-").append(year)
                   .append(" "));
    
                // set selected date into datepicker also
                dpResult.init(year, month, day, null);
    
            }
        };
    

    Refer Thsi link..May be this will help you.:- http://www.mkyong.com/android/android-date-picker-example/

    0 讨论(0)
  • 2021-01-13 11:17

    I think this is what you need

    // the call back received when the user "sets" the date in the dialog
    private DatePickerDialog.OnDateSetListener mDateSetListener =
        new DatePickerDialog.OnDateSetListener() {
            public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
                mYear = year;
                mMonth = monthOfYear;
                mDay = dayOfMonth;
    
            }
    };
    
    0 讨论(0)
  • 2021-01-13 11:17

    there's also a great tutorial here which also has source code to download.

    One thing to note, when compiling the source code, you need make SelectDateFragment class "static" in javapapers code as well as make populateSetDate() method static, and I also moved this code:

    mEdit = (EditText)findViewById(R.id.editText1);

    to the onCreate() method. After that you could be able to compile the code.

    MainActivity should look like this in the end after editing:

    package com.javapapers.andoiddatepicker;
    import java.util.Calendar;
    import android.app.DatePickerDialog;
    import android.app.Dialog;
    import android.os.Bundle;
    import android.support.v4.app.DialogFragment;
    import android.support.v4.app.FragmentActivity;
    import android.view.View;
    import android.widget.DatePicker;
    import android.widget.EditText;
    
    public class MainActivity extends FragmentActivity {
    
    private static EditText mEdit;
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    
        mEdit = (EditText) findViewById(R.id.editText1);
    }
    
    public void selectDate(View view) {
        DialogFragment newFragment = new SelectDateFragment();
        newFragment.show(getSupportFragmentManager(), "DatePicker");
    }
    
    public static void populateSetDate(int year, int month, int day) {
        mEdit.setText(month + "/" + day + "/" + year);
    }
    
    public static class SelectDateFragment extends DialogFragment implements
            DatePickerDialog.OnDateSetListener {
    
        @Override
        public Dialog onCreateDialog(Bundle savedInstanceState) {
            // Use the current date as the default date in the picker
            final Calendar c = Calendar.getInstance();
            int year = c.get(Calendar.YEAR);
            int month = c.get(Calendar.MONTH);
            int day = c.get(Calendar.DAY_OF_MONTH);
    
            // Create a new instance of DatePickerDialog and return it
            return new DatePickerDialog(getActivity(), this, year, month, day);
        }
    
        public void onDateSet(DatePicker view, int year, int month, int day) {
            populateSetDate(year, month+1, day);
        }
    }
    

    }

    0 讨论(0)
  • 2021-01-13 11:20

    Dj's answer would work. And if you want to use a calendar variable for storing the date then,

         public void onDateSet(DatePicker view, int year, int monthOfYear,
                int dayOfMonth) {
            setDate.set(year, monthOfYear, dayOfMonth);          
    

    Hope this helps!

    0 讨论(0)
  • 2021-01-13 11:20
    private void openCalendar() {
        DatePickerDialog dialog = new DatePickerDialog(getContext(), (view, year, month, dayOfMonth) -> {
    
            date = getDateString(year, month, dayOfMonth);
    
        }, mYear, mMonth, mDay);
        dialog.show();
    
    }
    
    private String getDateString(int year, int mMonth, int mDay) {
        Calendar calendar = Calendar.getInstance();
        calendar.set(year, mMonth, mDay);
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        return dateFormat.format(calendar.getTime());
    }
    
    0 讨论(0)
  • 2021-01-13 11:22

    There are 2 ways to do so:

    1. Using init() method of the DatePicker passing in OnDateChangedListener.

         datePicker.init(
              datePicker.getYear(), datePicker.getMonth(), datePicker.getDayOfMonth(),
              new DatePicker.OnDateChangedListener() {
                  @Override
                  public void onDateChanged(DatePicker datePicker, int year, int monthOfYear, int dayOfMonth) {
                      selectedYear = year;
                      selectedMonth = monthOfYear;
                      selectedDayOfMonth = dayOfMonth;
                      Log.d(LOG_TAG, "selectedDate = " + selectedYear  + "-" + selectedMonth + "-" + selectedDayOfMonth );
                     //TODO save or use selected value here
                  }//end onDateChangedListener
      
      });
      
    2. Having a Button click or overriding onPause to get currently selected date when a certain action is to be taken or when user is leaving the screen.

      @Override
      public void onPause() {
          int selectedYear = datePicker.getYear();
          int selectedMonth = datePicker.getMonth();
          int selectedDay =  datePicker.getDayOfMonth();
          //TODO save or use selected value here
      }
      
    0 讨论(0)
提交回复
热议问题