javajava-timeandroid-calendarlocaldate

How to set month and year with LocalDate?


enter image description here

I want change the month and year with these two numberpickers but I do not know how to change the date. What I want to do is this: when i click on OK button on BottomSheetDialog I want to set the month and year. Can you help me please? I tried but I couldn't find any solution on the internet. If you help me, I'll be appreciated. Thank you.

public class PlannerFragment extends Fragment implements CalendarAdapter.OnItemListener{

private TextView monthYearTextView;
private TextView monthYearPickerOKTextView;
private ImageView nextMonthImageView, previousMonthImageView;
private RecyclerView calendarRecyclerView;
private LocalDate selectedDate; /// tekrar buna bakılacak
private NumberPicker monthNumberPicker, yearNumberPicker;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    getActivity().setTitle("Planner");
    View v = inflater.inflate(R.layout.fragment_planner, container, false);

    previousMonthImageView = v.findViewById(R.id.previous_month_image_view);
    nextMonthImageView = v.findViewById(R.id.next_month_image_view);

    calendarRecyclerView = v.findViewById(R.id.calendar_recycler_view);
    monthYearTextView = v.findViewById(R.id.month_year_text_view);

    selectedDate = LocalDate.now();

    setMonthYear();

    previousMonthImageView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            selectedDate = selectedDate.minusMonths(1);
            setMonthYear();
        }
    });

    nextMonthImageView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            selectedDate = selectedDate.plusMonths(1);
            setMonthYear();
        }
    });

    monthYearTextView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            monthYearPicker(v);
        }
    });


    return v;
}

private void setMonthYear() {
    monthYearTextView.setText(monthYearFromDate(selectedDate));
    ArrayList<String> daysInMonth = daysInMonthArray(selectedDate);

    CalendarAdapter calendarAdapter = new CalendarAdapter(getActivity(), daysInMonth,
            this);
    RecyclerView.LayoutManager layoutManager = new GridLayoutManager(getActivity(), 7);
    calendarRecyclerView.setLayoutManager(layoutManager);
    calendarRecyclerView.setAdapter(calendarAdapter);
}

private ArrayList<String> daysInMonthArray(LocalDate date) {
    ArrayList<String> daysInMonthList = new ArrayList<>();
    YearMonth yearMonth = YearMonth.from(date);
    int daysInMonth = yearMonth.lengthOfMonth();
    LocalDate firstDayOfMonth = selectedDate.withDayOfMonth(1);
    int dayOfWeek = firstDayOfMonth.getDayOfWeek().getValue();
    if (dayOfWeek == 7){
        dayOfWeek = 1;
    } else {
        dayOfWeek++;
    }

    for (int i = 1; i <= 42; i++) {
        if (i < dayOfWeek || i >= daysInMonth + dayOfWeek){
            daysInMonthList.add("");
        } else {
            daysInMonthList.add(String.valueOf(i - dayOfWeek + 1));
        }
    }

    return daysInMonthList;
}

private String monthYearFromDate(LocalDate localDate) {
    DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MMMM yyyy");
    return localDate.format(formatter);
}

@Override
public void onItemClick(int position, String dayText) {
    LocalDate firstDayOfMonth = selectedDate.withDayOfMonth(1);
    int dayOfWeek = firstDayOfMonth.getDayOfWeek().getValue();
    if (dayOfWeek == 7){
        dayOfWeek = 1;
    } else {
        dayOfWeek++;
    }
    if (!dayText.equals("")){
        /*Toast.makeText(getActivity(), dayText + " " + monthYearFromDate(selectedDate),
                Toast.LENGTH_SHORT).show();*/
        Toast.makeText(getActivity(), String.valueOf(dayOfWeek),
                Toast.LENGTH_SHORT).show();
    }
}

public void monthYearPicker(View v){
    BottomSheetDialog bottomSheetDialog = new BottomSheetDialog(getActivity(),
            R.style.BottomSheetDialogTheme);
    View bottomSheetView = LayoutInflater.from(getActivity())
            .inflate(R.layout.month_and_year_picker_bottom_sheet_layout,
                    (ConstraintLayout) v.findViewById(R.id.month_year_picker_bottom_sheet_container));


    monthNumberPicker = bottomSheetView.findViewById(R.id.month_number_picker);
    yearNumberPicker = bottomSheetView.findViewById(R.id.year_number_picker);
    final Calendar calendar = Calendar.getInstance();

    Month.initMonths();
    monthNumberPicker.setMinValue(0);
    monthNumberPicker.setMaxValue(Month.getMonthArrayList().size() - 1);
    monthNumberPicker.setDisplayedValues(Month.monthNames());
    monthNumberPicker.setValue(calendar.get(Calendar.MONTH));

    yearNumberPicker.setMinValue(1984);
    yearNumberPicker.setMaxValue(2040);
    yearNumberPicker.setValue(calendar.get(Calendar.YEAR));

    bottomSheetView.findViewById(R.id.month_year_picker_ok_text_view).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            bottomSheetDialog.dismiss();
            selectedDate.withMonth(monthNumberPicker.getValue());
            setMonthYear();
            Toast.makeText(getActivity(), Month.getMonthArrayList().get(monthNumberPicker.getValue()).getName(),
                    Toast.LENGTH_SHORT).show();
        }
    });


    bottomSheetDialog.setContentView(bottomSheetView);
    bottomSheetDialog.show();


}

}


Solution

  • Good implementation, proper updated answer:

        LocalDate date1 = LocalDate.of(2021, Month.JANUARY, 1);
        LocalDate date2 = date1.withYear(2010);
        LocalDate date3 = date2.withMonth(Month.DECEMBER.getValue());
        LocalDate date4 = date3.withDayOfMonth(15);
        LocalDate date5 = date4.with(ChronoField.DAY_OF_YEAR, 100);
    

    Stolen from https://kodejava.org/how-do-i-manipulate-the-value-of-localdate-object/

    Bad outdated implementation, bad answer (just here for protocol)

    Simple pure Java implementation:

    You could also use the new java.time package, with classes like LocalDateTime etc.

    And then there's a myriad of Java Date Time libraries out there.

    Choose the way that works best for you.