scala 时间

  /**
    * 获取完整时间
    *
    * @return
    */
  def fullTime: String = {
    val today = new Date
    val f = new SimpleDateFormat("yyyyMMddHHmmss")
    f.format(today)
  }
  
  /**
    * 获取天时间
    *
    * @return
    */
  def dayTime: String = {
    val today = new Date
    val f = new SimpleDateFormat("yyyyMMdd")
    f.format(today)
  }

  def dirDayTime:String={
    val today = new Date
    val f = new SimpleDateFormat("yyyy/MM/dd")
    f.format(today)
  }
  
  /**
    * 增减日期
    * @param date
    * @param days
    * @return
    */
  def addDays(date:String,days:Int):String={
    val sdf = new SimpleDateFormat("yyyyMMdd")
    val d = sdf.parse(date)
    val calendar = Calendar.getInstance()
    calendar.setTime(d)
    calendar.add(Calendar.DATE, days)
    sdf.format(calendar.getTime)
  }

  /**
    * 增减月份
    * @param month
    * @param months
    * @return
    */
  def addMonth(month:String,months:Int):String={
    val sdf = new SimpleDateFormat("yyyyMM")
    val m = sdf.parse(month)
    val calendar = Calendar.getInstance()
    calendar.setTime(m)
    calendar.add(Calendar.MONTH, months)
    sdf.format(calendar.getTime)
  }
  
  
  
  /**
    * 获取月时间
    * @return
    */
  def monthTime:String={
    val today = new Date
    val f = new SimpleDateFormat("yyyyMM")
    f.format(today)
  }


  /**
    * 获取标准时间
    *
    * @return
    */
  def standardTime:String ={
    val currentTime: Date = new Date
    val formatter: SimpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
    formatter.format(currentTime)
  }


  /**
    * 获取标准时间
    *
    * @return
    */
  def standardTime:String ={
    val currentTime: Date = new Date
    val formatter: SimpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
    formatter.format(currentTime)
  }


  /**
    * 计算两个字符串的时间差,以分钟计
    * success
    *
    * @param d1
    * @param d2
    * @return
    */
  def calDateDiff(d1:String,d2:String):String={
    val diff = calDate(d1,d2)
    val hour = diff*1.0/(1000*60.0)
    //    println(s"时间差为:$hour 小时")
    hour.formatted("%.2f")
  }


  /**
    * 计算时间差
    *
    * @param d1
    * @param d2
    * @return
    */
  def calDate(d1:String,d2:String):Long={
    val date1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(d1)
    val date2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(d2)
    val time1 = date2.getTime
    val time2 = date1.getTime
    val diff = math.abs(time1-time2)
    diff
  }  
 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值