文章目錄
- 日期時間
- stream流
日期時間
jdk8新的日期時間類 解析和格式化DateTimeFormatter類(線程安全)
LocalDateTime類
Instant類
Duration類String time = "2013-02-11 11:00:00";DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss",Locale.CHINA);//日期時間轉字符串String format = dateTimeFormatter.format(LocalDateTime.now());System.out.println(format);//字符串轉日期時間LocalDateTime parse = LocalDateTime.parse(time, dateTimeFormatter);System.out.println(parse);/*** 獲取時間戳(UTC)時間*/@Testpublic void testInstant(){//獲取當前時間戳 時間戳已UTC 時間展示,與中國時間差距8小時Instant instant = Instant.now();System.out.println("UTC instant:"+instant);//如果想要獲取中國時間 可以通過設置偏移量來獲取OffsetDateTime offsetDateTime = instant.atOffset(ZoneOffset.ofHours(8));System.out.println("中國時間 offsetDateTime:"+offsetDateTime);}//毫秒時間戳
//toEpochMilli 獲取毫秒數
long l = instant.toEpochMilli();
System.out.println("使用instant獲取當前時間戳:"+l);
System.out.println("使用new Date()獲取當前時間戳"+new Date().getTime());
System.out.println("使用System.currentTimeMillis獲取當前時間戳:"+System.currentTimeMillis());//獲取時間間隔
//使用Duration 獲取兩個時間時間差@Testpublic void testDuration() throws InterruptedException {Instant instant = Instant.now();Thread.sleep(4899);Instant instant1 = Instant.now();Duration between = Duration.between(instant, instant1);System.out.println("毫秒:"+between.toMillis());System.out.println("秒:"+between.getSeconds());}
y:年份(例如,“yy” 表示年份的后兩位,“yyyy” 表示完整的年份)。
M:月份(1 到 12 或 01 到 12)。
d:日期(1 到 31 或 01 到 31)。
H:小時(0 到 23 或 00 到 23)。 //一般都是24小時
h:小時(1 到 12 或 01 到 12)。
m:分鐘(0 到 59或00到59)。
s:秒(0 到 59 或 00 到 59)。
S:毫秒。
stream流
//創建stream流
數組創建
Artrays.asStream
實現Collection接口的類創建
stream方法//過濾
filter()//聚合
max()
min()
count() //映射
Map()
flatMap() //接收一個stream流參數//歸約
reduce() //集合所有值歸約成一個值 //排序
sorted() //自定義排序規則//收集
collect()
收集里有很多操作,如
分組, Collectors.groupingBy() //確定值進行分組Collectors.partitionBy() // boolean值進行分組
歸約, Collectors.reduce()
連接, Collectors.join()
歸集, Collectors.asList()
待補充…