[英]Add days, android studio
In my application user can chose date with Calendar and then chose how many days he wants add. 在我的应用程序中,用户可以选择日历日期,然后选择要添加的天数。
Adding days working excellent, but I need to skip weekdays. 添加工作日非常好,但我需要跳过工作日。
For example you will choose day and add +15 days, result will be Saturday or Sunday but in this case I need to get always Monday if result is one of these days. 例如,您将选择天并添加+15天,结果将是星期六或星期日,但是在这种情况下,如果结果是其中几天之一,则我必须始终获取星期一。
Here is method for adding days 这是增加天数的方法
public static Date addDays(Date date, int days) {
Calendar cal = Calendar.getInstance();
cal.setTime(date);
cal.add(Calendar.DATE, days + FirstClass.hotovo);
return cal.getTime();
}
Thank you for your help, I am not expert in programming. 谢谢您的帮助,我不是编程专家。 I am amateur and i am still learning..
我是业余爱好者,我仍在学习..
You should check what is the resultant day ie check if its SATURDAY
or SUNDAY
and then adding 2 or 1 to get next MONDAY
. 您应该检查结果的星期
SATURDAY
即检查星期SATURDAY
还是SUNDAY
,然后加2或1以得到下一个MONDAY
。
NOTE: I do not know what is FirstClass.hotovo so I have removed temporary from below code and you can add it as you would have it in your project.
注意:我不知道什么是FirstClass.hotovo,所以我从下面的代码中删除了临时文件,您可以像在项目中那样添加它。 Below is to demonstrate how to check day and add 1 or 2 day respectively.
下面是演示如何检查日期并分别添加1或2天。
Here is the sample code. 这是示例代码。
Caller: 呼叫者:
addDays(new Date(), 18);
Your method: 您的方法:
public static Date addDays(Date date, int days) {
Calendar cal = Calendar.getInstance();
cal.setTime(date);
cal.add(Calendar.DATE, days);
Log.d("TEST", "BEFORE CHECKING: " + cal.getTime().toString());
// SATURDAY is the last day of week so add 2 days
if (cal.get(Calendar.DAY_OF_WEEK) == Calendar.SATURDAY) {
cal.add(Calendar.DATE, 2);
// SUNDAY is the first day of week so add 1 day
} else if (cal.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY) {
cal.add(Calendar.DATE, 1);
} // else not required as it means its one of the week day
Log.d("TEST", "AFTER UPDATING: " + cal.getTime().toString());
return cal.getTime();
}
Sample Run 样品运行
Resultant day was SATURDAY
so adding 2 days to get MONDAY
最后一天是
SATURDAY
所以加上2天即可获得MONDAY
07-25 15:46:55.729 4219-4219/? D/TEST: BEFORE CHECKING: Sat Aug 12 15:46:55 PDT 2017
07-25 15:46:55.729 4219-4219/? D/TEST: AFTER UPDATING: Mon Aug 14 15:46:55 PDT 2017
Resultant day was SUNDAY
so adding 1 day to get MONDAY
最终的日期是
SUNDAY
因此需要增加1天才能获得MONDAY
07-25 15:47:57.634 4322-4322/? D/TEST: BEFORE CHECKING: Sun Aug 13 15:47:57 PDT 2017
07-25 15:47:57.634 4322-4322/? D/TEST: AFTER UPDATING: Mon Aug 14 15:47:57 PDT 2017
Resultant day was TUESDAY
so not adding any more days 最终的日期是
TUESDAY
因此不再添加任何天数
07-25 15:52:27.115 4445-4445/? D/TEST: BEFORE CHECKING: Tue Aug 15 15:52:27 PDT 2017
07-25 15:52:27.115 4445-4445/? D/TEST: AFTER UPDATING: Tue Aug 15 15:52:27 PDT 2017
For an operation like this I definitely recommend using the modern Java date and time API. 对于这样的操作,我绝对建议您使用现代的Java日期和时间API。 Not only is it generally much nicer to work with than the outdated classes
Date
and Calendar
. 通常,它不仅比过时的
Date
和Calendar
类要好得多。 It also has some specific advantages in your situation: (1) It offers a LocalDate
that represents a date without time of day, which seems to model your data more precisely than the outdated Date
, which always includes time-of-day too. 在您遇到的情况中,它还具有一些特定的优势:(1)它提供了一个
LocalDate
,它表示没有日期的日期,这似乎比过时的Date
(总是也包括Date
更精确地对数据建模。 (2) Adding days and checking day-of-week is more convenient. (2)添加天数和检查星期几更为方便。
To use the modern API on Android you will need to get the ThreeTenABP, see this question: How to use ThreeTenABP in Android Project . 要在Android上使用现代API,您将需要获得ThreeTenABP,请参阅以下问题:如何在Android Project中使用ThreeTenABP 。 To use it in Java 8 or later, just dig in, it's built-in.
要在Java 8或更高版本中使用它,只需深入,它是内置的。
Your method becomes 您的方法成为
public static LocalDate addDays(LocalDate date, int days) {
date = date.plusDays(days + FirstClass.hotovo);
// weekend?
DayOfWeek dow = date.getDayOfWeek();
if (dow.equals(DayOfWeek.SATURDAY) || dow.equals(DayOfWeek.SUNDAY)) {
date = date.with(TemporalAdjusters.next(DayOfWeek.MONDAY));
}
return date;
}
Please note how clear and self-explanatory the code is compared to code using the old classes (if you think the part with the temporal adjuster looks a bit tricky, you may instead just use date.plusDays()
again; I wanted to demonstrate the temporal adjuster to give a bit of impression of the power of the newer classes). 请注意,将代码与使用旧类的代码相比,代码有多清晰和不言自明(如果您认为带有时间调整器的部分看起来有些棘手,则可以改用
date.plusDays()
;我想演示一下时间调整器,以给人一些较新类的力量的印象)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.