gpt4 book ai didi

java - Android日期选择器计算,特定日期之前和之后

转载 作者:行者123 更新时间:2023-12-02 07:26:06 25 4
gpt4 key购买 nike

我有一个应用程序,它使用当前日期和用户使用日期选择器选择的日期,如下所示:

  • 如果用户选择的日期超过当前日期+280天,一些代码将被执行。

  • 如果用户选择的日期小于当前日期,则某些代码将被执行。

我使用此代码来执行此操作..

    Calendar start2 = Calendar.getInstance();

int birthYear = birthDayDatePicker.getYear();
int birthMonth = birthDayDatePicker.getMonth();
int birthDay = birthDayDatePicker.getDayOfMonth();

start2.set(birthYear, birthMonth, birthDay);

Calendar cal2 = Calendar.getInstance();
cal2.setTime(birthDate);
cal2.add(Calendar.DAY_OF_MONTH,daysToAdd);
birthDayChosenCalender.set(birthYear,birthMonth,birthDay);
MaxBirthDayCalender.set(currentYear, currentMonth, currentDay);

long diff = birthDayChosenCalender.getTimeInMillis() - MaxBirthDayCalender.getTimeInMillis(); //result in millis
long daysBetween = diff / (24 * 60 * 60 * 1000);
System.out.println("Days between ,,,,,,,,,,,,,,,,,,"+daysBetween);

if(MaxBirthDayCalender.before(birthDayChosenCalender) && daysBetween <= 280){

do sth }

有没有其他干净的方法可以做到这一点!因为这种方式不太好用!

最佳答案

另一种简洁的方法是使用 Joda Time 库。

除此之外,一切都可以使用 millis 和单个日历实例来完成:

Calendar pickedDate = new GregorianCalendar(
picker.getYear(),
picker.getMonth(),
picker.getDayOfMonth());
long pickedTime = pickedDate.getTimeInMillis();
long now = new Date().getTime();

if (pickedTime - now <= (280 * 24 * 60 * 60 * 1000)) { // 280 days in milliseconds
// ...
}

应满足要求:

I have an application which use the current date and a date that is chose by user using date picker as follow:

  • If the date that the user chose is more than the current date +280 day, some code will be executed.
  • If the date that the user chose is less than the current date , some code will be executed.

关于java - Android日期选择器计算,特定日期之前和之后,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13561188/

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