0
点赞
收藏
分享

微信扫一扫

scala日期工具类

兵部尚输 2022-02-06 阅读 66
import java.text.SimpleDateFormat
import java.util.Date

object 日期工具类 {

  object DateUtils {

    var sdf: SimpleDateFormat = null

    // 日期转字符串
    def date2String(date: Date, template: String): String = {
      sdf = new SimpleDateFormat(template)
      sdf.format(date)
    }

    // 字符串转日期
    def string2Date(dateStr: String, template: String): Date = {
      sdf = new SimpleDateFormat(template)
      sdf.parse(dateStr)
    }

  }

  def main(args: Array[String]): Unit = {
    println(DateUtils.date2String(
      new Date(), "yyyy年MM月dd日 HH:mm:ss"))
    println(DateUtils.string2Date(
      "1314年11月11日", "yyyy年MM月dd日"))
  }

}
举报

相关推荐

0 条评论