时间日期转换工具类

  1. import java.text.SimpleDateFormat
  2. import java.util.Date
  3. /**
  4. * @author :ywb
  5. * @date :Created in 2022/1/13 6:43 下午
  6. * @description:toDo
  7. */
  8. object DataTime {
  9. object DateUtil{
  10. var simple : SimpleDateFormat = _
  11. def DateToString(d:Date, template:String ):String={
  12. simple = new SimpleDateFormat(template)
  13. simple.format(d)
  14. }
  15. def StringToDate(d:String,template:String):Date={
  16. simple = new SimpleDateFormat(template)
  17. simple.parse(d)
  18. }
  19. }
  20. def main(args: Array[String]): Unit = {
  21. var d = new Date()
  22. val str = DateUtil.DateToString(d, "yyyy-MM-dd HH:mm:ss")
  23. println(str)
  24. val date = DateUtil.StringToDate("2022年5月20日","yyyy年MM月dd日")
  25. println(date)
  26. }
  27. }

image.png