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) }

Tags: