前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >java8时间处理

java8时间处理

作者头像
海向
发布2020-05-31 15:13:23
8140
发布2020-05-31 15:13:23
举报
文章被收录于专栏:海向海向
  /**
     * Instant:瞬时实例
     * LocalDate:本地日期 不包含具体时间 例如 2020-02-02 可以用来记录纪念日
     * LocalDTime:本地时间 不包含日期
     * LocalDateTime:结合了日期和时间 但不包含时差和时区
     * ZonedDateTime:完整的日期时间,包含时区和相对UTC或格林威治的视察
     *
     * ZoneOffSet,ZoneId:操作时区
     *
     * DateTimeFormatter:格式化时间
     *
     * LocalDate等许多类为 final 线程安全不可变, plusHour withDay等操作后要用新的对象来接收
     */
    public static void main(String[] args) {
        LocalDate today = LocalDate.now();
        System.out.println(today); // 2020-05-30

        /* 处理特定日期  */
        LocalDate birthday = LocalDate.of(1994,8,11);
        System.out.println(birthday); // 1994-08-11

        /* 日期比较  */
        if (today.equals(birthday)) {
            System.out.println("日期形同");
        }

        /* MonthDay 和 YearMonth 用来检查某一天是不是生日这种周期性时间 */
        MonthDay myBithday = MonthDay.of(birthday.getMonth(), birthday.getDayOfMonth());
        MonthDay currentMonDay = MonthDay.from(today);
        if (myBithday.equals(currentMonDay)) {
            System.out.println("今天是你的生日");
        }

        /* 在现有时间基础上囧算之前之后的年、月、周、时、分、秒 */
        LocalDateTime nowTime = LocalDateTime.now();
        LocalDateTime afterTime = nowTime.plusHours(3);
        LocalDateTime beforeTime = nowTime.minusYears(2);
        System.out.println("afterTime:"+afterTime+"- beforeTime:"+beforeTime);
        // afterTime:2020-05-30T15:09:28.013- beforeTime:2018-05-30T12:09:28.013


        /* 在现有时间基础上修改年、月、周、时、分、秒 */
        LocalDateTime localDateTime = nowTime.withHour(11);
        System.out.println("with 11 Time:"+localDateTime); // with 11 Time:2020-05-30T11:09:28.013


        /* 使用java8的Clock时钟类获取时间戳 */
        Clock clock = Clock.systemUTC();
        System.out.println(clock.millis()); // 1590811768013
        System.out.println(System.currentTimeMillis()); // 1590811768013

        Clock defalutClock = Clock.systemDefaultZone();
        System.out.println(defalutClock.millis()); // 1590811768013
        System.out.println(defalutClock.getZone()); // Asia/Shanghai


        /* 判断早于某时还是晚于某时 */
        LocalDateTime tomorrow = nowTime.plusDays(1);
        if (tomorrow.isEqual(nowTime)) {
            //时间一样
        } else if (tomorrow.isAfter(nowTime)) {
            //是否在当前之后
        } else if (tomorrow.isBefore(nowTime)) {
            //之前
        } else {
            //
        }

        /* 时区处理 */
        ZoneId beijing = ZoneId.of("America/New_York");
        LocalDateTime timenow = LocalDateTime.now();

        ZonedDateTime zonedDateTime = ZonedDateTime.of(timenow, beijing);
        System.out.println("纽约时间" + zonedDateTime);
        // 纽约时间2020-05-30T12:22:38.102-04:00[America/New_York]


        /* 检测闰年 */
        LocalDate leafYear = LocalDate.now();
        if (leafYear.isLeapYear()){
            //闰年
        }

        /* 计算两个时间之间的天数和月数 */
        LocalDate current = LocalDate.now();
        LocalDate yourBirthday = LocalDate.of(1996, 8, 11);
        Period between = Period.between(yourBirthday, current);
        System.out.println(between.getYears()+"年"+between.getMonths()+"月"+between.getDays()+"天");
        // 23年9月19天

        /* 包含时差信息的日期和时间
        *  ZoneOffset 表示时差 印度与GMT或者UTC飙车事件时差+5:30
        * */
        LocalDateTime dateTime = LocalDateTime.now();
        ZoneOffset offset = ZoneOffset.of("+05:30");
        OffsetDateTime offsetDateTime = OffsetDateTime.of(dateTime, offset);
        System.out.println(offsetDateTime); //2020-05-30T12:48:54.655+05:30
        System.out.println(OffsetDateTime.now()); //2020-05-30T12:48:54.655+08:00

        /* 格式化时间 */

        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        System.out.println(formatter.format(LocalDateTime.now()));//2020-05-30 12:55:40

    }
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2020-05-30 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档