MaterialDatePicker get selected dates

前端 未结 4 1068
春和景丽
春和景丽 2020-11-30 14:36

I\'m calling a MaterialDatePicker like this in Android:

MaterialDatePicker.Builder> builder = MaterialDatePicker.Builder.dateRa         


        
相关标签:
4条回答
  • 2020-11-30 14:50

    The answer of GR Envoy is good, but I want to change a bit. It would be better to set time zone to UTC.

    private val outputDateFormat = SimpleDateFormat("dd.MM.yyyy", Locale.getDefault()).apply {
        timeZone = TimeZone.getTimeZone("UTC")
    }
    
    ...
    picker.addOnPositiveButtonClickListener {
        val text = outputDateFormat.format(it)
    }
    
    0 讨论(0)
  • 2020-11-30 14:56

    Just use the addOnPositiveButtonClickListener listener called when the user confirms a valid selection:

    For a single date picker:

    picker.addOnPositiveButtonClickListener(new MaterialPickerOnPositiveButtonClickListener<Long>() {
          @Override public void onPositiveButtonClick(Long selection) {
            // Do something...
          }
        });
    

    For a range date picker:

    MaterialDatePicker<Pair<Long, Long>> pickerRange = builderRange.build();
    pickerRange.show(....);
    
    pickerRange.addOnPositiveButtonClickListener(new MaterialPickerOnPositiveButtonClickListener<Pair<Long, Long>>() {
      @Override public void onPositiveButtonClick(Pair<Long,Long> selection) {
           Long startDate = selection.first;
           Long endDate = selection.second;
           //Do something...
      }
    });
    
    0 讨论(0)
  • 2020-11-30 15:01

    For those that struggle with this and the fact that their timestamp is off a day, here is my working solution. I have a requirement of API 23 so I could not use any of the nice Epoch functions in java.time.*. The key for me was realizing I need to do the timezone offset math.

            picker.addOnPositiveButtonClickListener(new MaterialPickerOnPositiveButtonClickListener<Long>() {
            @Override
            public void onPositiveButtonClick(Long selectedDate) {
                // user has selected a date
                // format the date and set the text of the input box to be the selected date
                // right now this format is hard-coded, this will change
                ;
                // Get the offset from our timezone and UTC.
                TimeZone timeZoneUTC = TimeZone.getDefault();
                // It will be negative, so that's the -1
                int offsetFromUTC = timeZoneUTC.getOffset(new Date().getTime()) * -1;
    
                // Create a date format, then a date object with our offset
                SimpleDateFormat simpleFormat = new SimpleDateFormat("MM/dd/yyyy", Locale.US);
                Date date = new Date(selectedDate + offsetFromUTC);
    
                dataEntry.setText(simpleFormat.format(lDate));
            }
        });
        picker.show(myActivity.getSupportFragmentManager(), picker.toString());
    
    0 讨论(0)
  • 2020-11-30 15:13

    I searched around and found out many Material DatePicker Range get selected dates solutions were deprecated.

    I am using dateRangePicker in my project, so below codes are solely for dateRangePicker() because I need to obtain both start date and end date.

    In my Java activity code:

                materialDatePicker.addOnPositiveButtonClickListener(new MaterialPickerOnPositiveButtonClickListener() {
                @Override
                public void onPositiveButtonClick(Object selection) {
    //                Get the selected DATE RANGE
                    Pair selectedDates = (Pair) materialDatePicker.getSelection();
    //              then obtain the startDate & endDate from the range
                    final Pair<Date, Date> rangeDate = new Pair<>(new Date((Long) selectedDates.first), new Date((Long) selectedDates.second));
    //              assigned variables
                    Date startDate = rangeDate.first;
                    Date endDate = rangeDate.second;
    //              Format the dates in ur desired display mode
                    SimpleDateFormat simpleFormat = new SimpleDateFormat("dd MMM yyyy");
    //              Display it by setText
                    datedisplay.setText("SELECTED DATE : " +  simpleFormat.format(startDate) + " Second : " + simpleFormat.format(endDate));
                }
    
            });
    

    Sample output :

    0 讨论(0)
提交回复
热议问题