栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

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

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

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
  }


}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/963532.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号