1.问题描述:开发中遇到以下需求:
根据给出的时间戳,求出 此时间戳对应的 年 月 日 周 的时间戳
如 年 给出这年1号00:00:00 的时间戳
周 给出这周周一的00:00:00的时间戳
下面是方法代码
def getAggregateDate(day: Long, timeUnit: String): Long = {
val date: DateTime = new DateTime(day*1000, DateTimeZone.forID("Asia/Shanghai"))
val valueTime = timeUnit match {
case "day" => date.toString("yyyy-MM-dd")
case "week" => date.withDayOfWeek(1).toString("yyyy-MM-dd")
case "month" => date.withDayOfMonth(1).toString("yyyy-MM-dd")
case "year" => date.withDayOfYear(1).toString("yyyy-MM-dd")
}
val format = new SimpleDateFormat("yyyy-MM-dd")
val timeStamp: Long = format.parse(valueTime).getTime
timeStamp/1000
}