Scala笔记
- 2020 年 1 月 19 日
- 筆記
Scala笔记
1.四种操作符的区别和联系
- :: 该方法成为cons,表时构造,向队列头部加入元素。x::list表示向list头部加入元素。(列表构造: 12::1::2::"bar"::"foo" 表示List[Any]= (2,1,2,bar,foo)
- :+和+:表示分别在尾部加入元素和在头部加入元素。
- ++ 表示连接两个集合
- ::: 该方法只能用于连接两个list类型的集合
2.日期操作(经常用到,所以记录下)
- 获取今天0点时间戳 12val dateFormat = new SimpleDateFormat("yyyy-MM-dd")val cur = dateFormat.parse(dateFormat.format(new Date())).getTime
- 日期格式转时间戳 123val dateFormat = new SimpleDateFormat("yyyy-MM-dd")//val dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:MM:ss")val timestamp = dateFormat.parse(dateFormat.format(new Date())).getTime
- 时间戳转日期 12val dateFormat = new SimpleDateFormat("yyyy-MM-dd")val date = dateFormat.format(new Date())
3.删除目录或文件
123456789101112131415161718 |
import java.io.Filedef dirDel(path: File) { if (!path.exists()) return else if (path.isFile()) { path.delete() return } val file: Array[File] = path.listFiles() for (d <- file) { dirDel(d) } path.delete() } if(Files.exists(Paths.get(path))) { // 先删除目录里的文件 dirDel(Paths.get(path).toFile) } |
---|