获取当前时间的上一个自然周的开始时间:
import java.text.SimpleDateFormat
import java.util.{Calendar, Date}
def getLastWeek():String={
var list=List(("","",""))
list = list.init
var num = 7
val date= new Date()
var week = new Date().getDay()
if(week == 0){//周天的时候
num += 7
week = 7
}else{
num = num + week - 1
}
val newDate = new Date(date.getTime() - num*24*60*60*1000);//获得上周一的时间戳
val dateFormat = new SimpleDateFormat("yyyy-MM-dd")
val cal: Calendar = Calendar.getInstance()
cal.setTime(newDate)
var monthstr: String = ""
var yearstr: String = ""
yearstr = cal.get(Calendar.YEAR) + ""
if (cal.get(Calendar.MONTH) + 1 < 10) {
monthstr = "0" + (cal.get(Calendar.MONTH) + 1) + ""
} else {
monthstr = (cal.get(Calendar.MONTH) + 1) + ""
}
val time1 = yearstr+"-"+ monthstr +"-"+cal.get(Calendar.DATE)//上一周的开始日期
time1
}
获取当前时间的上一个自然周集合:
def getLastWeek():List[(String,String,String)]={
var list=List(("","",""))
list = list.init
var num = 7
val date= new Date()
var week = new Date().getDay()
if(week == 0){//周天的时候
num += 7
week = 7
}else{
num = num + week - 1
}
val dateFormat = new SimpleDateFormat("yyyy-MM-dd")
for(i <- week to num) {
val newDate2 = new Date(date.getTime() - i * 24 * 60 * 60 * 1000)
val cals: Calendar = Calendar.getInstance()
cals.setTime(newDate2)
var monthstrs: String = ""
var yearstrs: String = ""
yearstrs = cals.get(Calendar.YEAR) + ""
if (cals.get(Calendar.MONTH) + 1 < 10) {
monthstrs = "0" + (cals.get(Calendar.MONTH) + 1) + ""
} else {
monthstrs = (cals.get(Calendar.MONTH) + 1) + ""
}
list.::=(yearstrs, monthstrs, dateFormat.format(cals.getTime))
}
list
}
获取当前时间的上一个月的第一天与最后一天
def getLastMonth():(String,String,String,String)={
val dateFormat = new SimpleDateFormat("yyyy-MM-dd")
val cals: Calendar = Calendar.getInstance()
cals.add(Calendar.MONTH,-1)
var monthstrs: String = ""
var yearstrs: String = ""
yearstrs = cals.get(Calendar.YEAR) + ""
if (cals.get(Calendar.MONTH) + 1 < 10) {
monthstrs = "0" + (cals.get(Calendar.MONTH) + 1) + ""
} else {
monthstrs = (cals.get(Calendar.MONTH) + 1) + ""
}
cals.set(Calendar.DAY_OF_MONTH,1)
val firstDay=dateFormat.format(cals.getTime())
//获取前月的最后一天
val cale:Calendar = Calendar.getInstance()
cale.set(Calendar.DAY_OF_MONTH,0)
val lastDay=dateFormat.format(cale.getTime())
(yearstrs,monthstrs,firstDay,lastDay)
}