scala日期工具类

阅读 66

2022-02-06

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)

0 0 举报