我试图从当前日期撤退一定天数;它可以工作到24天,但在25天后它会增加几天而不是撤退...
你们中的任何人都可以理解为什么吗?
import java.sql.Timestamp
val retreve_days: ((Int) => java.sql.Timestamp) = (nb_days: Int) => {
new java.sql.Timestamp(System.currentTimeMillis()- nb_days*24*60*60*1000)
}
val today = retreve_days(0)
// today: java.sql.Timestamp = 2019-03-01 15:43:41.418
val minus1d = retreve_days(1)
// minus1d: java.sql.Timestamp = 2019-02-28 15:43:41.958
val minus10d = retreve_days(10)
// minus10d: java.sql.Timestamp = 2019-02-19 15:43:42.502
val minus20d = retreve_days(20)
// minus20d: java.sql.Timestamp = 2019-02-09 15:43:42.895
val minus23d = retreve_days(23)
//minus23d: java.sql.Timestamp = 2019-02-06 15:43:43.245
val minus24d = retreve_days(24)
//minus24d: java.sql.Timestamp = 2019-02-05 15:43:43.577
val minus25d = retreve_days(25)
//minus25d: java.sql.Timestamp = 2019-03-26 08:46:31.817
谢谢!
答案 0 :(得分:2)
对于25
类型,Int
天的计算量太大:
25 * 24 * 60 * 60 * 1000
// mathematical value is 2160000000
Int.MaxValue
// 2147483647
// we can see the calculation is larger, this is why it becomes negative, therefore *adding* more time.
您可以采取的补救措施是将其更改为Long
以进行适当的计算,这可以通过在适当的位置添加.toLong
来完成:
val retrieve_days: Int => java.sql.Timestamp = (nb_days: Int) => {
new java.sql.Timestamp(System.currentTimeMillis()- nb_days.toLong*24*60*60*1000)
}
val minus25d = retrieve_days(25)
// minus25d: java.sql.Timestamp = 2019-02-04 10:09:48.613