日期时间
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)时间
*/
@Test
public 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 获取两个时间时间差
@Test
public 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()
待补充…