1

I'm using a DatePicker where I wont to get the selected Date. I have tried to set the Data before getting it.

<DatePicker
            android:id="@+id/datepicker"
            android:layout_width="0dp"
            android:layout_height="80dp"
            android:calendarViewShown="false"
            android:datePickerMode="spinner"
            android:day="@={viewmodel.day}"
            android:month="@={viewmodel.month}"
            android:year="@={viewmodel.year}" />

I have set the DatePicker to a spinner. I'm passing every single data "Day", "Month" and "Year" to a MutableLiveData object.

public class DateViewModel{
    private MutableLiveData<Integer> day = new MutableLiveData<>();
    private MutableLiveData<Integer> month = new MutableLiveData<>();
    private MutableLiveData<Integer> year = new MutableLiveData<>();
}

I tried to initialiaz the data and it is showing me the right dates.

public void setDate(){
        day.setValue(Calendar.getInstance().get(Calendar.DAY_OF_MONTH));
        month.setValue(Calendar.getInstance().get(Calendar.MONTH));
        year.setValue(Calendar.getInstance().get(Calendar.YEAR));
}

I'm trying to set the data into a Date Objekt.

  Date crtDate;
        Integer cYear = year != null ? year.getValue() : 0;
        Integer cMonth = month != null ? month.getValue() : 0;
        Integer cDay = day != null ? day.getValue() : 0;
        if (cDay != null && cMonth != null && cYear != null) {
            crtDate= new Date(cYear, cMonth, cDay);
            filterData.setSpinnerDate(crtDate);
        }

Now when i try to get the same Date back without changing anything on the spinner it gives me different results.

  System.out.println("-----------> Day: " + filterData.getSpinnerDate.getDay());
  System.out.println("-----------> Month: " + filterData.getSpinnerDate.getMonth());
  System.out.println("-----------> Year: " + filterData.getSpinnerDate.getYear());

By outputing the values it gives me different values. Thanks in advance

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Anas G
  • 11
  • 1

0 Answers0