Java-常用模块

文章目录

日期时间

jdk8新的日期时间类 解析和格式化DateTimeFormatter类(线程安全)
LocalDateTimeInstantDurationString 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()

待补充…

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值