scala 使用时间戳与时间互相转换类

借助java中的Date与Calendar实现时间类型互相转换

  • 时间戳:与1970年间隔的毫秒数-1621337596326
  • 时间string类型常见形式:“2021-05-18 19:33:16”
    1.获取当前时间
    2.获取昨天日期
    3.计算时间差
    4.指定日期和间隔天数,返回指定日期前N天的日期 date - N days
    5.指定日期和间隔小时,返回指定日期前N小时的日期 date - N days
    6.指定日期和间隔小时,返回指定日期后N小时的日期 date - N days
    7.时间与时间戳互相转换
    8.根据时间戳获取 DAY_OF_WEEK
    代码实现如下:
package com.sf.spark.util

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

/**
  * Created by Jinhui Yang  on 2018/7/17 11:23
  * Modified by: 
  * Version: 0.0.1
  * Usage: 
  *
  */
object DateUtil extends Serializable {
  /**
    * 获取当前系统时间,时间格式为:yyyy-MM-dd HH:mm:ss.SSS
    * @return yyyy-MM-dd HH:mm:ss.SSS
    */
  def getNowDate:String={
    val now = new Date()
    now.getTime
    val dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS")
    val result = dateFormat.format( now )
    result
  }
  /**
    * 获取昨天日期
    * @return
    */
  def getYesterday():String= {
    val dateFormat: SimpleDateFormat = new SimpleDateFormat("yyyy-MM-dd")
    val cal: Calendar = Calendar.getInstance()
    cal.add(Calendar.DATE, -1)
    val yesterday = dateFormat.format(cal.getTime)
    yesterday
  }
  /**
    * 计算时间差
    * @param start_time
    * @param end_Time
    * @return 相差小时/h
    */
  def getDiffTime(start_time:String,end_Time: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(start_time)
    val end:Date = df.parse(end_Time)
    val between:Long=(end.getTime-begin.getTime)/1000//转化成秒
    val hour:Float=between.toFloat/3600
    hour
//    val decf:DecimalFormat=new DecimalFormat("#0.00")
//    decf.format(hour)//格式化
  }
  /**
    * 指定日期和间隔天数,返回指定日期前N天的日期 date - N days
    * @param dt
    * @param interval
    * @return
    */
  def getDaysBefore(dt: Date, interval: Int):String = {
    val dateFormat: SimpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")

    val cal: Calendar = Calendar.getInstance()
    cal.setTime(dt)

    cal.add(Calendar.DATE, - interval)
    val yesterday = dateFormat.format(cal.getTime)
    yesterday
  }
  /**
    * 指定日期和间隔小时,返回指定日期后N小时的日期 date - N days
    * @param dt
    * @param interval
    * @return
    */
  def getHoursAfter(dt: Date, interval: Int):String = {
    val dateFormat: SimpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")

    val cal: Calendar = Calendar.getInstance()
    cal.setTime(dt)

    cal.add(Calendar.HOUR, + interval)
    val yesterday = dateFormat.format(cal.getTime)
    yesterday
  }
  /**
    * 指定日期和间隔小时,返回指定日期前N小时的日期 date - N days
    * @param dt
    * @param interval
    * @return
    */
  def getHoursBefore(dt: Date, interval: Int):String = {
    val dateFormat: SimpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")

    val cal: Calendar = Calendar.getInstance()
    cal.setTime(dt)

    cal.add(Calendar.HOUR, - interval)
    val yesterday = dateFormat.format(cal.getTime)
    yesterday
  }
  /**
    * 时间转换为时间戳
    * @param tm
    * @return
    */
  def tranTimeToLong(tm:String) :Long={
    val fm = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
    val dt = fm.parse(tm)
    val aa = fm.format(dt)
    val tim: Long = dt.getTime
    tim
  }

  def tranTimeToDate(tm:String) :Date={
    val newtime :Date = new SimpleDateFormat("yyyyMMdd").parse(tm)
    newtime
  }


  /**
    * 时间戳转化为时间
    * @param tm
    * @return
    */
  def tranTimeToString(tm:String) :String={
    val fm = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
    val tim = fm.format(new Date(tm.toLong))
    tim
  }
  /**
   * 时间戳转化为时间
   *
   * @param tm
   * @return
   */
  def tranTimeToString(tm: Long): String = {
    val fm = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
    val tim = fm.format(new Date(tm))
    tim
  }
  /**
    * 根据时间戳获取 DAY_OF_WEEK
    * @param tm
    * @return
    */
  def getWeek(tm:Long): Int ={
    val cal: Calendar = Calendar.getInstance()
    cal.setTimeInMillis(tm)
    val o = cal.get(Calendar.DAY_OF_WEEK)
    val week = if(o==Calendar.SUNDAY) 7 else o-1
    week
  }
}


 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值