【scala】获取当前时间的上一个自然周以及自然周集合;获取当前时间的上一个自然月以及自然月的第一天与最后一天

6 篇文章 0 订阅
2 篇文章 0 订阅

 获取当前时间的上一个自然周的开始时间:

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)
   }

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值