工具類-時間工具類

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"))
  }
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章