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