gpt4 book ai didi

java - 如何使用 LocalDate 设置月份和年份?

转载 作者:行者123 更新时间:2023-12-02 18:15:29 24 4
gpt4 key购买 nike

enter image description here

我想用这两个数字选择器更改月份和年份,但我不知道如何更改日期。我想要做的是:当我点击 BottomSheetDialog 上的确定按钮时,我想设置月份和年份。你能帮我吗?我试过了,但在互联网上找不到任何解决方案。如果你帮助我,我将不胜感激。谢谢。

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();


}

最佳答案

良好的实现,正确的更新答案:

    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);

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

错误的过时实现,错误的答案(仅用于协议(protocol))

简单的纯Java实现:

  • 使用日历。创建一个新实例;您可能需要公历:Calendar.getInstance();
  • 将日历值设置为日期:cal.setTime(pDate);
  • 调整单个字段:cal.add(pField, pValue);cal.set(pField, pValue);
  • 检索日期对象:cal.getTime();

您还可以使用新的 java.time 包,其中包含 LocalDateTime 等类。

还有大量的 Java 日期时间库。

选择最适合您的方式。

关于java - 如何使用 LocalDate 设置月份和年份?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71650710/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com