您的位置 首页 教育

scala中对时间进行处理的常用方法汇总

import java.text.SimpleDateFormat import java.util.{Cal…

import java.text.SimpleDateFormat
import java.util.{Calendar, Date, Random}

object TimeDateUtil {

  def getNowDate():String={
    var now:Date = new Date()
    var  dateFormat:SimpleDateFormat = new SimpleDateFormat("yyyyMMdd")
    var today = dateFormat.format( now )
    today
  }

  def getNowDateWithMiddleHorizontaLine():String={
    var now:Date = new Date()
    var  dateFormat:SimpleDateFormat = new SimpleDateFormat("yyyy-MM-dd")
    var today = dateFormat.format( now )
    today
  }
  def getTomorrow():String= {
    var dateFormat: SimpleDateFormat = new SimpleDateFormat("yyyyMMdd")
    var cal: Calendar = Calendar.getInstance()
    cal.add(Calendar.DATE, +1)
    var tomorrow = dateFormat.format(cal.getTime())
    tomorrow
  }
  def getTomorrowWithMiddleHorizontaLine():String= {
    var dateFormat: SimpleDateFormat = new SimpleDateFormat("yyyy-MM-dd")
    var cal: Calendar = Calendar.getInstance()
    cal.add(Calendar.DATE, +1)
    var tomorrow = dateFormat.format(cal.getTime())
    tomorrow
  }
  def getDayAfterTomorrow():String={
    var dateFormat: SimpleDateFormat = new SimpleDateFormat("yyyyMMdd")
    var cal: Calendar = Calendar.getInstance()
    cal.add(Calendar.DATE, +2)
    var  dayAfterTomorrow = dateFormat.format(cal.getTime())
    dayAfterTomorrow
  }

  def getCurrentHour(): String= {

    val now:Date = new Date()
    val  dateFormat:SimpleDateFormat = new SimpleDateFormat("yyyyMMdd-HH:mm:ss")
    val currentHour = dateFormat.format(now).substring(9,11)

    currentHour.toString
  }

  def getCurrentTime():String={
    val now:Date = new Date()
    val  dateFormat:SimpleDateFormat = new SimpleDateFormat("yyyyMMddHH:mm:ss")
    dateFormat.format(now).substring(0,10)
  }

  
  def getDiffTime(startTime:String,endTime:String)={
    val df:SimpleDateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
    //    val df:SimpleDateFormat=new SimpleDateFormat("HH:mm:ss")
    val begin:Date=df.parse(startTime)
    val end:Date = df.parse(endTime)
    val between:Long=(end.getTime-begin.getTime)/1000//转化成秒
//    val hour:Float=between.toFloat/3600
    between
    //    val decf:DecimalFormat=new DecimalFormat("#0.00")
    //    decf.format(hour)//格式化
  }


  def getRandomInt(Min: Int, Max: Int): Int = {
    val rand = new Random
    rand.nextInt(Max - Min + 1) + Min
  }


}

本文来自网络,不代表0514资讯网立场,转载请注明出处:https://0514zx.com/info/6371.html
0514zx.com

作者: 0514zx.com

优质职场领域创作者
联系我们

联系我们

工作时间:周一至周五,9:00-17:30,节假日休息

关注微信
微信扫一扫关注我们

微信扫一扫关注我们

关注微博
返回顶部