Duncan's Blog

Scala笔记

Scala笔记

1.四种操作符的区别和联系

  • :: 该方法成为cons,表时构造,向队列头部加入元素。x::list表示向list头部加入元素。(列表构造:

    1
    2::1::2::"bar"::"foo" 表示List[Any]= (2,1,2,bar,foo)
  • :+和+:表示分别在尾部加入元素和在头部加入元素。

  • ++ 表示连接两个集合

  • ::: 该方法只能用于连接两个list类型的集合

2.日期操作(经常用到,所以记录下)

  • 获取今天0点时间戳

    1
    2
    val dateFormat = new SimpleDateFormat("yyyy-MM-dd")
    val cur = dateFormat.parse(dateFormat.format(new Date())).getTime
  • 日期格式转时间戳

    1
    2
    3
    val 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
  • 时间戳转日期

    1
    2
    val dateFormat = new SimpleDateFormat("yyyy-MM-dd")
    val date = dateFormat.format(new Date())

3.删除目录或文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
import java.io.File
def 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)
}
分享