scala of single objects _ Tools Case

Case tools
needs
to write a special tools for formatting DateUtil date and time
to define a method for the date (Date) is converted to the date string, for example: 2030-10-05
steps
define a singleton object DateUtil, the method defined date format (the format)
used to convert the date string SimpleDateFormat
reference code
scala object _10ObjectDemo { object DateUtils { // 在object中定义的成员变量,相当于Java中定义一个静态变量 // 定义一个SimpleDateFormat日期时间格式化对象 val simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm") // 相当于Java中定义一个静态方法 def format(date: Date) = simpleDateFormat.format(date) } // main是一个静态方法,所以必须要写在object中 def main(args: Array[String]): Unit = { println(DateUtils.format(new Date())) } }
Here Insert Picture Description
answer:
Here Insert Picture Description

Published 139 original articles · won praise 333 · views 210 000 +

Guess you like

Origin blog.csdn.net/qq_45765882/article/details/104302403