java8 时间日期api

jdk8之前日期是线程不安全的

public static void main(String[] args) throws Exception{
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        Callable<Date> task = new Callable<Date>() {
            @Override
            public Date call() throws Exception {
                return sdf.parse("20161218");
            }
        };

        ExecutorService pool = Executors.newFixedThreadPool(10);
        List<Future<Date>> futures = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            futures.add(pool.submit(task));
        }
        for (Future<Date> future : futures  ) {
            System.out.println(future.get());
        }
    }

在这里插入图片描述

使用LocalDate,LocalTime, LocalDateTime

LocalDate,LocalTime, LocalDateTime类的实例是不可变的对象,分别表示使用ISO-8601日历系统的日期,时间,日期和时间,他们提供了简单的日期,时间,日期和时间,提供了简单的日期或时间,并不包含当前的时间信息,也不包含与时区相关的信息

/**
     * 2020-09-20
     * 2020-09-20T12:14:34
     * 2022-09-20
     * 2020-08-30
     * 2020
     * SEPTEMBER
     * 9
     * 20
     * SUNDAY
     * 264
     */
    @Test
    public void test1(){

        LocalDate localDate = LocalDate.now();
        System.out.println(localDate);

        LocalDateTime localDateTime = LocalDateTime.of(2020, 9, 20, 12, 14, 34);
        System.out.println(localDateTime);

        LocalDate localDate1 = localDate.plusYears(2);
        System.out.println(localDate1);

        LocalDate localDate2 = localDate.minusDays(21);
        System.out.println(localDate2);

        System.out.println(localDate.getYear());
        System.out.println(localDate.getMonth());
        System.out.println(localDate.getMonthValue());
        System.out.println(localDate.getDayOfMonth());
        System.out.println(localDate.getDayOfWeek());
        System.out.println(localDate.getDayOfYear());

    }
/**
     * Instant: 时间戳(以Unix 元年1970年1月1日00:00:00 到莫个时刻的时间毫秒值)
     */
    @Test
    public void test2(){

        Instant instant = Instant.now(); //默认获取UTC时区
        System.out.println(instant);

        OffsetDateTime offsetDateTime = instant.atOffset(ZoneOffset.ofHours(8));
        System.out.println(offsetDateTime);

        System.out.println(instant.toEpochMilli());

        Instant instant1 = Instant.ofEpochMilli(60);
        System.out.println(instant1);

    }

    /**
     * duration 计算两个“时间”之间的间隔
     * period 计算两个“日期”之间的间隔
     */

    @Test
    public void test3(){

        Instant instant = Instant.now();
        try {
            TimeUnit.SECONDS.sleep(1);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        Instant instant1 = Instant.now();
        Duration duration = Duration.between(instant, instant1);
        System.out.println(duration.toMillis());

        System.out.println("----------------");

        LocalTime localTime = LocalTime.now();
        try {
            TimeUnit.SECONDS.sleep(1);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        LocalTime localTime1 =  LocalTime.now();
        System.out.println(Duration.between(localTime, localTime1).toMillis());


        System.out.println("---------------------");

        LocalDate localDate = LocalDate.of(2015, 1, 1);
        LocalDate localDate1 =  LocalDate.now();
        Period period = Period.between(localDate, localDate1);
        System.out.println(period);
        System.out.println(period.getYears());
        System.out.println(period.getMonths());
    }

============================
在这里插入图片描述

 /**
     * 格式化时间/日期
     */
    @Test
    public void test6(){
        DateTimeFormatter dtf = DateTimeFormatter.ISO_DATE;
        LocalDate localDate = LocalDate.now();
        String format = localDate.format(dtf);
        System.out.println(format);

        System.out.println("-----------------");

        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy年MM月dd日 HH:mm:ss");
        String format1 = dateTimeFormatter.format(LocalDateTime.now());
        System.out.println(format1);

        LocalDate localDate1 = LocalDate.now();
        LocalDate localDate2 = localDate1.parse(format1, dateTimeFormatter);
        System.out.println(localDate2);
    }

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值