LocalDateTime plusDays()在月底时不起作用
LocalDateTime plusDays() dont work when is the end of the month
我今天(5 月 30 日)使用自定义小时和分钟测试我的应用程序:
var today=LocalDateTime.now().withHour(hour).withMinute(minute).withSecond(0).withNano(0)
但是,当我这样做时:
today=today.plusDays(1) //today is: 2022-03-30T10:04
在日志中是return:
2022-03-30T10:04
UPDATE:这是完整的代码。我正在 Android 8.1.0
上进行测试
fun calcEndTime(hour: Int, minute: Int, initinmilliseconds:Long):Long {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
var endtime =LocalDateTime.now().withHour(hour).withMinute(minute).withSecond(0).withNano(0)
val now = LocalDateTime.now().withSecond(0).withNano(0)
val inittime = Instant.ofEpochMilli(initinmilliseconds).atZone(ZoneOffset.UTC).toLocalDateTime()
val days=inittime.dayOfMonth-now.dayOfMonth
if(days==1||days<0){
endtime=endtime.plusDays(1)
Log.d("ZXCV","if ${endtime}")
}
if (endtime.isBefore(now)) {
endtime = endtime.plusDays(1)
}
return endtime.toInstant(ZoneOffset.UTC).toEpochMilli()
}
}
这样试过,好像还不错
fun calcEndTime(hour: Int, minute: Int, initinmilliseconds: Long): LocalDateTime {
var endtime = LocalDateTime.now().withHour(hour).withMinute(minute).withSecond(0).withNano(0)
val now = LocalDateTime.now().withSecond(0).withNano(0)
val inittime = Instant.ofEpochMilli(initinmilliseconds).atZone(ZoneOffset.UTC).toLocalDateTime()
val days = inittime.dayOfMonth - now.dayOfMonth
if (days == 1 || days < 0) {
endtime = endtime.plusDays(1)
Log.d("ZXCV", "if ${endtime}")
}
if (endtime.isBefore(now)) {
endtime = endtime.plusDays(1)
Log.d("ZXCV2", "if ${endtime}")
}
return endtime//.toInstant(ZoneOffset.UTC).toEpochMilli() // try commenting this out to return LocalDateTime instead
}
示例用法:
binding.buttonFirst.setOnClickListener {
val hour = 10
val minute = 4
var today = calcEndTime(hour, minute, 1648706400000)
var test = "TESTING today" + "\n"
test += today.toString() + "\n"
today = today.plusDays(1)
test += today.toString() + "\n"
binding.textviewFirst.text = test
}
截图
我今天(5 月 30 日)使用自定义小时和分钟测试我的应用程序:
var today=LocalDateTime.now().withHour(hour).withMinute(minute).withSecond(0).withNano(0)
但是,当我这样做时:
today=today.plusDays(1) //today is: 2022-03-30T10:04
在日志中是return:
2022-03-30T10:04
UPDATE:这是完整的代码。我正在 Android 8.1.0
上进行测试fun calcEndTime(hour: Int, minute: Int, initinmilliseconds:Long):Long {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
var endtime =LocalDateTime.now().withHour(hour).withMinute(minute).withSecond(0).withNano(0)
val now = LocalDateTime.now().withSecond(0).withNano(0)
val inittime = Instant.ofEpochMilli(initinmilliseconds).atZone(ZoneOffset.UTC).toLocalDateTime()
val days=inittime.dayOfMonth-now.dayOfMonth
if(days==1||days<0){
endtime=endtime.plusDays(1)
Log.d("ZXCV","if ${endtime}")
}
if (endtime.isBefore(now)) {
endtime = endtime.plusDays(1)
}
return endtime.toInstant(ZoneOffset.UTC).toEpochMilli()
}
}
这样试过,好像还不错
fun calcEndTime(hour: Int, minute: Int, initinmilliseconds: Long): LocalDateTime {
var endtime = LocalDateTime.now().withHour(hour).withMinute(minute).withSecond(0).withNano(0)
val now = LocalDateTime.now().withSecond(0).withNano(0)
val inittime = Instant.ofEpochMilli(initinmilliseconds).atZone(ZoneOffset.UTC).toLocalDateTime()
val days = inittime.dayOfMonth - now.dayOfMonth
if (days == 1 || days < 0) {
endtime = endtime.plusDays(1)
Log.d("ZXCV", "if ${endtime}")
}
if (endtime.isBefore(now)) {
endtime = endtime.plusDays(1)
Log.d("ZXCV2", "if ${endtime}")
}
return endtime//.toInstant(ZoneOffset.UTC).toEpochMilli() // try commenting this out to return LocalDateTime instead
}
示例用法:
binding.buttonFirst.setOnClickListener {
val hour = 10
val minute = 4
var today = calcEndTime(hour, minute, 1648706400000)
var test = "TESTING today" + "\n"
test += today.toString() + "\n"
today = today.plusDays(1)
test += today.toString() + "\n"
binding.textviewFirst.text = test
}
截图