首页 > 其他分享 >判断时间格式

判断时间格式

时间:2022-10-13 16:14:44浏览次数:50  
标签:判断 MM dd else yyyy 时间 str date 格式

def convertDateFormat(str : String) : String  = {
  val date  = if (str ! = null && str.nonEmpty && str.trim.contains( " " ))
    str.split( " " )( 0 )
  else str

  val newDate  = if (date.nonEmpty && date.contains( "/" )) {
    val isYear  = date.split( "/" )( 0 )
    if (isYear.length  == 4 )
      DateTimeFormat.forPattern( "yyyy/MM/dd" ).parseLocalDateTime(date).toString( "yyyy-MM-dd" )
    else
      DateTimeFormat.forPattern( "MM/dd/yyyy" ).parseLocalDateTime(date).toString( "yyyy-MM-dd" )
  }  else {
    if (date.nonEmpty) {
      if (!date.contains( "-" ))
        Try(DateTimeFormat.forPattern( "yyyyMMdd" ).parseLocalDateTime(date).toString( "yyyy-MM-dd" )).getOrElse(date)
      else {
        val isYear  = date.split( "-" )( 0 )
        if (isYear.length == 4 ){
          date
        } else {
          Try(DateTimeFormat.forPattern( "MM-dd-yyyy" ).parseLocalDateTime(date).toString( "yyyy-MM-dd" )).getOrElse(date)
        }
      }
    }  else date
  }
  if (newDate.contains( "0000" ))  "" else newDate
}

  

标签:判断,MM,dd,else,yyyy,时间,str,date,格式
From: https://www.cnblogs.com/xiaohanxqh/p/16788457.html

相关文章