我创建了一个财务应用程序,我希望该应用程序向用户显示财务月份。财务月份通常代表用户获得工资或最大收入的日子。那一天可以是每月的第一天,每月的最后一天,或者只是每个月的 15 号。此值可在设置中配置。
我尝试运行在我的 ViewModel 中调用的两种方法,getFirstDayOfMonth
并且getLastDayOfMonth
为了更好地理解上下文,这里有一些例子,我们将今天的日期作为参考。2020 年 9 月 9 日。输入值是我从设置中读取的值,用户可以从中选择,是从 1 到 31 的数字。
示例:输入:5 输出:2020 年 9 月 5 日 00:00 开始,2020 年 10 月 4 日 23:59:59 结束
=================
输入:31 输出:2020 年 8 月 31 日 00:00 开始,2020 年 9 月 30 日 23:59:59
问题是如果月份没有那一天,它会得到左边最接近的一天,例如,如果选择31作为第一天并且该月份有30天,则将计算30作为第一天,同样对于结束日期,如果选择了 31 并且我们在 2 月并且它只有 28 天,则将选择 28。
到目前为止,我有这段代码,但我觉得它可以改进,也没有按预期工作。
fun getFirstDayOfMonth(date: LocalDateTime): Long {
var tempDate = date
val firstDayOfMonth = lastDay?.filter { it.isDigit() }!!.toInt()
if (firstDayOfMonth < tempDate.dayOfMonth) {
tempDate = tempDate.withDayOfMonth(firstDayOfMonth)
} else if (firstDayOfMonth > tempDate.dayOfMonth) {
tempDate = tempDate.minusMonths(1)
if (tempDate.monthValue == 12) {
//I don't know why minusMonths does not work in the same way as plusMonths, when I write .plusMonths(1) it also change the year if I am in december, with minusMonths if I am in January it does not change the year to minus one year.
tempDate = tempDate.minusYears(1)
}
if (firstDayOfMonth > tempDate.with(TemporalAdjusters.lastDayOfMonth()).dayOfMonth) {
tempDate.withDayOfMonth(tempDate.with(TemporalAdjusters.lastDayOfMonth()).dayOfMonth)
} else {
tempDate = tempDate.withDayOfMonth(firstDayOfMonth)
}
}
return tempDate.atZone(ZoneId.systemDefault()).toInstant().toEpochMilli()
}
fun getLastDayOfMonth(date: LocalDateTime): Long {
var tempDate = date
val firstDayOfMonth = lastDay?.filter { it.isDigit() }!!.toInt()
if (firstDayOfMonth > tempDate.dayOfMonth && firstDayOfMonth <= tempDate.with(
TemporalAdjusters.lastDayOfMonth()
).dayOfMonth
) {
tempDate = tempDate.withDayOfMonth(firstDayOfMonth).minusDays(1)
} else {
tempDate = tempDate.plusMonths(1)
if (firstDayOfMonth > tempDate.with(TemporalAdjusters.lastDayOfMonth()).dayOfMonth) {
tempDate.withDayOfMonth(tempDate.with(TemporalAdjusters.lastDayOfMonth()).dayOfMonth)
} else {
tempDate = tempDate.withDayOfMonth(firstDayOfMonth).minusDays(1)
}
}
return tempDate.atZone(ZoneId.systemDefault()).toInstant().toEpochMilli()
}
例如,我编写了一个运行今天日期的测试,只将月份从 1 月更改为 12 月。这是当前算法的输出。每月 1 日是用户选择的日期,该日期在应用程序中也是默认设置。
Todays date 09.01.2020
Running for month January
Running for 1 st of the month
01.01.2020
31.01.2020
===========================================
Todays date 09.02.2020
Running for month February
Running for 1 st of the month
01.02.2020
29.02.2020
===========================================
Todays date 09.03.2020
Running for month March
Running for 1 st of the month
01.03.2020
31.03.2020
===========================================
Todays date 09.04.2020
Running for month April
Running for 1 st of the month
01.04.2020
30.04.2020
===========================================
Todays date 09.05.2020
Running for month May
Running for 1 st of the month
01.05.2020
31.05.2020
===========================================
Todays date 09.06.2020
Running for month June
Running for 1 st of the month
01.06.2020
30.06.2020
===========================================
Todays date 09.07.2020
Running for month July
Running for 1 st of the month
01.07.2020
31.07.2020
===========================================
Todays date 09.08.2020
Running for month August
Running for 1 st of the month
01.08.2020
31.08.2020
===========================================
Todays date 09.09.2020
Running for month September
Running for 1 st of the month
01.09.2020
30.09.2020
===========================================
Todays date 09.10.2020
Running for month October
Running for 1 st of the month
01.10.2020
31.10.2020
===========================================
Todays date 09.11.2020
Running for month November
Running for 1 st of the month
01.11.2020
30.11.2020
===========================================
Todays date 09.12.2020
Running for month December
Running for 1 st of the month
01.12.2020
31.12.2021
===========================================
Process finished with exit code 0
也为 31 选择
Todays date 09.01.2020
Running for month January
Running for 31 th of the month
31.12.2019
30.01.2020
===========================================
Todays date 09.02.2020
Running for month February
Running for 31 th of the month
31.01.2020
30.03.2020
===========================================
Todays date 09.03.2020
Running for month March
Running for 31 th of the month
09.02.2020
30.03.2020
===========================================
Todays date 09.04.2020
Running for month April
Running for 31 th of the month
31.03.2020
30.05.2020
===========================================
Todays date 09.05.2020
Running for month May
Running for 31 th of the month
09.04.2020
30.05.2020
===========================================
Todays date 09.06.2020
Running for month June
Running for 31 th of the month
31.05.2020
30.07.2020
===========================================
Todays date 09.07.2020
Running for month July
Running for 31 th of the month
09.06.2020
30.07.2020
===========================================
Todays date 09.08.2020
Running for month August
Running for 31 th of the month
31.07.2020
30.08.2020
===========================================
Todays date 09.09.2020
Running for month September
Running for 31 th of the month
31.08.2020
30.10.2020
===========================================
Todays date 09.10.2020
Running for month October
Running for 31 th of the month
09.09.2020
30.10.2020
===========================================
Todays date 09.11.2020
Running for month November
Running for 31 th of the month
31.10.2020
30.12.2021
===========================================
Todays date 09.12.2020
Running for month December
Running for 31 th of the month
09.11.2020
30.12.2021
===========================================
Process finished with exit code 0