Java8日期处理

Java8 推出了新的日期API

序号 描述
1 Instant 时间戳
2 Duration 持续时间,时间差
3 LocalDate 只包含日期,例如:2022-09-19
4 LocalTime 只包含时间,例如:10:01:20
5 LocalDateTime 包含时间和日期,例如:2022-09-19 10:01:20
6 Period 时间段
7 ZoneOffset 时区偏移量
8 ZonedDateTime 带时区的时间
9 Clock 时钟,获取当时的时间戳,或当前时区下的日期时间信息
10 java.time.format.DateTimeFormatter 时间格式化

代码示例:

点击查看代码
package com.date;

import java.time.*;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;

public class DateDemo {
    public static void main(String[] args) {
        //获取今天
        LocalDate now = LocalDate.now();
        System.out.println(now);// 2022-09-19
        //获取年月日
        int year = now.getYear();
        int month = now.getMonthValue();
        int day = now.getDayOfMonth();
        System.out.println("year:" + year);//   year:2022
        System.out.println("month:" + month);// month:9
        System.out.println("day:" + day);//     day:19
        //自定义日期,需要传入年,月,日作为参数
        LocalDate date = LocalDate.of(2022, 9, 29);
        System.out.println(date);// 2022-09-29
        //判断日期相等
        if(date.equals(now)){
            System.out.println("相等");
        }else{
            System.out.println("不等");// 不等
        }
        //检查类似生日这种周期性事件
        MonthDay monthDay = MonthDay.of(date.getMonthValue(), date.getDayOfMonth());
        MonthDay currentMonthDay = MonthDay.from(now);
        System.out.println(monthDay+"-----"+currentMonthDay);
        if(currentMonthDay.equals(monthDay)){
            System.out.println("是你的生日");
        }else{
            System.out.println("没有到");// 没有到
        }
        //获取当前时间,无日期
        LocalTime localTime = LocalTime.now();
        System.out.println(localTime);// 10:47:16.691
        //当前时间加3个小时
        LocalTime plusHours = localTime.plusHours(3);
        //当前时间加3分钟
        LocalTime plusMinutes = localTime.plusMinutes(3);
        //当前时间加3秒
        LocalTime plusSeconds = localTime.plusSeconds(3);
        //当前时间加3纳秒
        LocalTime plusNanos = localTime.plusNanos(3);
        //当前年月日加一周,同样的加一年、一个月或几年几个月,只需要换第二个参数
        LocalDate plusWeeks = now.plus(1, ChronoUnit.WEEKS);
        System.out.println("三小时后:"+plusHours); // 三小时后:13:47:16.691
        System.out.println("一周后的日期:"+plusWeeks); // 一周后的日期:2022-09-26
        //计算一年前的日期
        LocalDate lastYear = now.minus(1, ChronoUnit.YEARS);
        System.out.println(lastYear);// 2021-09-19
        //Clock
        Clock clock = Clock.systemUTC();
        //获取时间戳
        System.out.println(clock.millis());// 1663555636691
        //根据系统时区获取
        Clock systemDefaultZone = Clock.systemDefaultZone();
        System.out.println(systemDefaultZone.millis()); // 1663555636691
        //判断给定的日期是大于某天还是小于某天
        LocalDate tomorrow = LocalDate.of(2022, 9, 20);
        if(tomorrow.isAfter(now)){
            System.out.println(tomorrow);// 2022-09-20
        }
        LocalDate yesterday = now.minus(1, ChronoUnit.DAYS);
        if (yesterday.isBefore(now)){
            System.out.println(yesterday);// 2022-09-18
        }
        //处理时区,下面的例子展示将本时区的时间转化为另一个时区
        ZoneId zoneId = ZoneId.of("America/New_York");
        LocalDateTime localDateTime = LocalDateTime.now();
        ZonedDateTime zonedDateTime = ZonedDateTime.of(localDateTime, zoneId);
        System.out.println(zonedDateTime);//2022-09-19T10:26:07.061-04:00[America/New_York]
        //时间间隔(两个日期间的天数和月数)
        Period between = Period.between(now, date);
        System.out.println(between.getDays());//2022-9-29 与 2022-9-19相差10天
        System.out.println(between.getMonths());//0
        //获取当前时间戳
        Instant instant = Instant.now();
        System.out.println(instant.toEpochMilli());// 1663555636695
        //使用预定义的格式化工具格式化日期
        String d = "20220920";
        LocalDate parse = LocalDate.parse(d, DateTimeFormatter.BASIC_ISO_DATE);
        System.out.println(parse);//2022-09-20
        //日期转字符串
        LocalDateTime dateTime = LocalDateTime.now();
        DateTimeFormatter pattern = DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss");
        String s = dateTime.format(pattern);
        System.out.println(s);// 2022/09/19 10:47:16
        //字符串转日期
        LocalDate localDate = LocalDate.parse(s, pattern);
        System.out.println(localDate);//2022-09-19
    }
}

posted @ 2022-09-19 10:54  修白  阅读(140)  评论(0)    收藏  举报