工具类-时间工具类

package com.cartravel.tools

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

import org.apache.commons.lang.time.FastDateFormat
import org.apache.commons.lang3.StringUtils

/**
  * 时间工具类
  */
object TimeUtils {
  def getData(timeStamp:String , format:String):String = {
    val time = new Date()
    /*
    这里使用的是FastDateFormat而不是SimpleDateFormat
    因为SimpleDateFormat线程是不安全的,
    有千分之的概率会出现2019-12-17 10(这就出现了一条脏数据):2019-12-17 10:20:25
     */
    val instance = FastDateFormat.getInstance(format)
    val format1 = instance.format(time)
    format1
  }

  def formatYYYYmmdd(time:String):Option[String] = {
    //yyyy-MM-dd HH-mm-ss  -->yyyyMMddHHmmss
    if(StringUtils.isNotBlank(time) && time != ""){
      val fields: Array[String] = time.split(" ")
      if(fields.length > 1){
        Some(fields(0).replace("-","") + fields(1).replace(":" , ""))
      }else{
        None
      }
    }else{
      None
    }
  }
  def YYYYmmdd(time:String):Option[String] = {
    //yyyy-MM-dd HH-mm-ss  -->yyyyMMddHHmmss
    if(StringUtils.isNotBlank(time) && time != ""){
        Some(time.replace("-",""))
    }else{
      None
    }
  }
  //获取当前日志天
  def getNowData():String = {
    val time = new Date()
    val instance = FastDateFormat.getInstance("yyyyMMdd")
    val format1 = instance.format(time)
    format1
  }
  //获取当前日期分钟
  def getNowDataMin():String = {
    val time = new Date()
    val instance = FastDateFormat.getInstance("yyyyMMddHHmm")
    val format1 = instance.format(time)
    format1
  }
  def getNowDataSS():String = {
    val time = new Date()
    val instance = FastDateFormat.getInstance("yyyyMMddHHmmss")
    val format1 = instance.format(time)
    format1
  }

  //给定日期,返回15天之前时间
  def getHalfMonthdate(time:String): String ={
    val  c = Calendar.getInstance()
    c.setTime(new SimpleDateFormat("yyyyMMddHHmm").parse(time))
    val day1 = c.get(Calendar.DATE)
    c.set(Calendar.DATE, day1 - 15)
    val dayAfter = new SimpleDateFormat("yyyyMMddHHmm").format(c.getTime())
    dayAfter

  }
  //返回推送订单和有效订单的表名称结尾
  def getTableDate(): Any ={
    val cal = Calendar.getInstance
    val year = cal.get(Calendar.YEAR)
    var month = cal.get(Calendar.MONTH)
    if(month < 10 ) {
      cal.get(Calendar.YEAR).toString + 0+(cal.get(Calendar.MONTH) + 1).toString
    }else if(month > 10){
      cal.get(Calendar.YEAR).toString + (cal.get(Calendar.MONTH) + 1).toString
    }

  }
  //时间戳转换成时间
  def tranTimeToString(tm:String) :String={
    val fdm = FastDateFormat.getInstance("yyyy-MM-dd HH:mm:ss")
    val time = fdm.format(new Date(tm.toLong))
    time
  }


  def main(args: Array[String]): Unit = {

    println(tranTimeToString("1568025609176"))
  }
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值