1) When you click on a datapicker for a selection, a dialog pops up in which the date is current and not set in TextView . Method in the documentation did not find something. I explain: I choose the year 2000, everything is displayed, I click again and I want to push back from 2000, not 2017 again. This is necessary for convenience when I click on certain years in a row and get results over the network.

Made on the basis of dialogue fragment. In activation, the result is sent through kalbek.

 public class DatePickerFragment extends DialogFragment implements DatePickerDialog.OnDateSetListener { DialogFragmentListener mCallback; Boolean timesCalled = true; // Используется как костыль. при нажатии на DataPiker он вызывается дважды. @Override public Dialog onCreateDialog(Bundle savedInstanceState) { final Calendar calendar = Calendar.getInstance(); int year = calendar.get(Calendar.YEAR); int month = calendar.get(Calendar.MONTH); int day = calendar.get(Calendar.DAY_OF_MONTH); final Calendar calendarMin = Calendar.getInstance(); // Дата для установки нижнего порога в DataPiker calendarMin.set(1999, 0, 1); // http://fixer.io/ хранит курсы валют начиная с 1999 года . Отсчет месяцев идет с нуля. DatePickerDialog datePickerDialog = new DatePickerDialog(getActivity(), this, year, month, day); DatePicker dp = datePickerDialog.getDatePicker(); dp.setMinDate(calendarMin.getTimeInMillis()); // ставим нижний диапазон в DatePiker dp.setMaxDate(System.currentTimeMillis()); // верхний диапазон в DatePiker устанавливаем сегодняшним днем return datePickerDialog; } @Override public void onDateSet(DatePicker view, int year, int month, int day) { if (timesCalled == true) { Calendar calendar = Calendar.getInstance(); calendar.set(year, month, day); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); String formattedDate = sdf.format(calendar.getTime()); mCallback.getDate(formattedDate); timesCalled = false; } } interface DialogFragmentListener { //узнать зачем нужен вложенный интерфейс void getDate(String date); } @Override public void onAttach(Activity activity) { super.onAttach(activity); try { mCallback = (DialogFragmentListener) activity; } catch (ClassCastException e) { throw new ClassCastException(activity.toString() + " must implement DialogFragmentListener"); } } @Override public void onDetach() { super.onDetach(); timesCalled = true; // возвращаем исходное значение mCallback = null; } } 

The method that takes kalbek in activi

 public void getDate(String date) { Current_Date.setText(date); } 
  • See this answer - a fully working solution. The question should contain only one problem. If you have several problems, ask a few questions. - pavlofff

1 answer 1

On the first, everything is correct, because you will receive the current date and pass it year, month, day to this function.

 DatePickerDialog datePickerDialog = new DatePickerDialog(getActivity(), this, year, month, day); 

You need to get the set value from the field and parse it, like so

 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); String getgatestr = ((Button)v).getText().toString(); if(! getgatestr.isEmpty()){ String[] arrdate = getgatestr.split("\\."); day = Integer.parseInt(arrdate[0]); month = Integer.parseInt(arrdate[1])-1; year = Integer.parseInt(arrdate[2]); } 

On the second question, first write the date of execution of this code in order to understand from what date to do the calculations, because on different days of launch these will be yours on December 2 and 3 on other days. In general, in onDateSet, draw a conclusion to the log of the data that the function received, the month may not be displayed, not from zero, you need to make a conclusion to the log.

  • You should not be encouraged to break the rules of this resource and answer several problems in one question. The author will issue a second question and you can answer it. The fact is that this is not a forum, but a Q & A, the goal of which is not to help a specific person, but to create a base for solving problems. And several problems and answers to them in one question complicate the search for the right solution to people with similar problems in the future - pavlofff
  • pavlofff There was not a solution to the problem, but simply a tip, so that when creating a new question a person clarified what I wrote. - Abramov Alexey