JDK1.8 新增的日期时间API

LocalDate、 LocalTime、 LocalDateTime类的实例是不可变的对象,分别表示使用 ISO-8601日历系统的日期、时间、日期和时间。
它们提供了简单的日期或时间,并不包含当前的时间信息。也不包含与时区相关的信息。
注: ISO-8601日历系统是国际标准化组织制定的现代公民的日期和时间的表示法
这些新增的日期时间API都在 java.time包下

  • 获取对象的方法
    方式1通过静态方法 now();
    例如:LocalDateTime ldt = LocalDateTime.now();

    方式2通过静态方法of()方法参数可以指定年月日时分秒
    例如:LocalDateTime of = LocalDateTime.of(2018, 12, 30, 20, 20, 20);

  • 常用方法
    (1).与获取相关的方法:get系类的方法
    ldt.getYear();获取年
    ldt.getMinute();获取分钟
    ldt.getHour();获取小时
    getDayOfMonth 获得月份天数(1-31)
    getDayOfYear 获得年份天数(1-366)
    getDayOfWeek 获得星期几(返回一个 DayOfWeek枚举值)
    getMonth 获得月份, 返回一个 Month 枚举值
    getMonthValue 获得月份(1-12)
    getYear 获得年份
    (2).格式化日期日期字符串的方法 format()
    例如:String yyyy = ldt.format(DateTimeFormatter.ofPattern(“yyyy”));
    (3).转换的方法 toLocalDate();toLocalTime();
    例如:LocalDate localDate = ldt.toLocalDate();
    例如:LocalTime localTime = ldt.toLocalTime();
    (4).判断的方法
    isAfter()判断一个日期是否在指定日期之后
    isBefore()判断一个日期是否在指定日期之前
    isEqual(); 判断两个日期是否相同
    isLeapYear()判断是否是闰年注意是LocalDate类中的方法
    例如: boolean after = ldt.isAfter(LocalDateTime.of(2024, 1, 1, 2, 3));
    例如 boolean b= LocalDate.now().isLeapYear();

    (5).解析的静态方法parse(“2007-12-03T10:15:30”);
    paser() 将一个日期字符串解析成日期对象,注意字符串日期的写法的格式要正确,否则解析失败
    例如:LocalDateTime parse = LocalDateTime.parse(“2007-12-03T10:15:30”);
    按照我们指定的格式去解析:

    注意细节:
    如果用LocalDateTime 想按照我们的自定义的格式去解析,注意日期字符串的 年月日时分秒要写全,不然就报错
    LocalDateTime ldt4 = LocalDateTime.now();
    DateTimeFormatter formatter2 = DateTimeFormatter.ofPattern(“yyyy-MM-dd HH:mm:ss”);
    LocalDateTime.parse(“2018-01-21 20:30:36”, formatter2);

    (6).添加年月日时分秒的方法 plus系列的方法 都会返回一个新的LocalDateTime的对象
    LocalDateTime localDateTime = ldt.plusYears(1);
    LocalDateTime localDateTime1 = ldt.plusMonths(3);
    LocalDateTime localDateTime2=ldt.plusHours(10);
    (7).减去年月日时分秒的方法 minus 系列的方法 注意都会返回一个新的LocalDateTime的对象
    例如:LocalDateTime localDateTime2 = ldt.minusYears(8);
    (8).指定年月日时分秒的方法 with系列的方法 注意都会返回一个新的LocalDateTime的对象
    例如 LocalDateTime localDateTime3 = ldt.withYear(1998);
    //获取这个月的第几个星期几是几号,比如 TemporalAdjusters.dayOfWeekInMonth(2, DayOfWeek.FRIDAY) 代表的意思是这个月的第二个星期五是几号
    // TemporalAdjusters.dayOfWeekInMonth(2, DayOfWeek.FRIDAY)
    LocalDateTime with1 = now.with(TemporalAdjusters.dayOfWeekInMonth(2,DayOfWeek.FRIDAY));

Instant 时间戳类从1970-01-01 00:00:00 截止到当前时间的毫秒值

(1) 获取对象的方法 now():
注意默认获取出来的是当前的美国时间和我们相差八个小时
Instant ins = Instant.now();
System.out.println(ins);
我们在东八区 所以可以加8个小时 就是我们的北京时间
(2) Instant中设置偏移量的方法:atOffset() 设置偏移量:
OffsetDateTime time = ins.atOffset(ZoneOffset.ofHours(8));
System.out.println(time);
(3) 获取系统默认时区时间的方法atZone():
方法的参数是要一个时区的编号可以通过时区编号类获取出来
ZoneId.systemDefault()获取本地的默认时区ID
ZonedDateTime zonedDateTime = ins.atZone(ZoneId.systemDefault());
System.out.println(zonedDateTime);
(4) get系列的方法:
getEpochSecond() 获取从1970-01-01 00:00:00到当前时间的秒值
toEpochMilli();获取从1970-01-01 00:00:00到当前时间的毫秒值
getNano()方法是把获取到的当前时间的秒数 换算成纳秒
long epochSecond = ins.getEpochSecond();//获取从1970-01-01 00:00:00到当前时间的秒值
getNano()方法是把获取到的当前时间的豪秒数 换算成纳秒 比如当前时间是2018-01-01 14:00:20:30
那就把30豪秒换算成纳秒 int nano = ins.getNano();
(5) ofEpochSecond()方法 给计算机元年增加秒数
ofEpochMilli() 给计算机元年增加毫秒数
例如 Instant instant = Instant.ofEpochSecond(5);
System.out.println(instant);
单位换算
1 毫秒 = 1000 微妙 = 1000000 纳秒

Period : 用于计算两个“日期”间隔的类、Duration : 用于计算两个“时间”间隔的类

  • Duration类中静态方法between()
    Instant start = Instant.now();
    for(int i=0;i<1000L;i++){
    System.out.println(“循环内容”);
    }
    Instant end = Instant.now();
    静态方法:between() 计算两个时间的间隔,默认是秒
    Duration between = Durati’on.between(start, end);
    Duration中的toMillis()方法:将秒转成毫秒
    System.out.println(between.toMillis());

  • Period类 中的静态方法between()
    计算两个日期之间的间隔
    LocalDate s = LocalDate.of(1985, 03, 05);
    LocalDate now = LocalDate.now();
    Period be = Period.between(s, now);
    System.out.println(be.getYears());间隔了多少年
    System.out.println(be.getMonths());间隔了多少月
    System.out.println(be.getDays());间隔多少天

TemporalAdjuster : 时间校正器,是个接口

一般我们用该接口的一个对应的工具类 TemporalAdjusters中的一些常量,来指定日期
例如:
LocalDate now = LocalDate.now();
System.out.println(now);
(1) 使用TemporalAdjusters自带的常量来设置日期
LocalDate with = now.with(TemporalAdjusters.lastDayOfYear());
System.out.println(with);
(2) 采用TemporalAdjusters中的next方法来指定日期
LocalDate date = now.with(TemporalAdjusters.next(DayOfWeek.SUNDAY));
System.out.println(date);
例如:TemporalAdjusters.next(DayOfWeek.SUNDAY) 本周的星期天
例如:TemporalAdjusters.nextOrSame(DayOfWeek.MONDAY) 下一周的星期一
(3) 采用自定义的方式来指定日期 比如指定下个工作日
LocalDateTime ldt = LocalDateTime.now();
LocalDateTime workDay = ldt.with(new TemporalAdjuster() {
@Override
public Temporal adjustInto(Temporal temporal) {
//向下转型
LocalDateTime ld = (LocalDateTime) temporal;
//获取这周的星期几
DayOfWeek dayOfWeek = ld.getDayOfWeek();
if (dayOfWeek.equals(DayOfWeek.FRIDAY)) {
return ld.plusDays(3);//如果这天是星期五,那下个工做日就加3天
} else if (dayOfWeek.equals(DayOfWeek.SATURDAY)) {
return ld.plusDays(2);//如果这天是星期六,那下个工做日就加2天
} else {
//其他就加一天
return ld.plusDays(1);
}
}
});
System.out.println(workDay);

DateTimeFormatter :解析和格式化日期或时间的类

  • 获取对象的方式,通过静态方法ofPattern(“yyyy-MM-dd”);
    DateTimeFormatter dateFormat = DateTimeFormatter.ofPattern(“yyyy-MM-dd”);
    LocalDateTime now = LocalDateTime.now();

  • format()方法把一个日期对象的默认格式 格式化成指定的格式
    String format1 = dateFormat.format(now);
    System.out.println(format1);

  • 格式化日期 方式2使用日期类中的format方法 传入一个日期格式化类对象
    LocalDateTime now1 = LocalDateTime.now();
    使用日期类中的format方法 传入一个日期格式化类对象
    使用DateTimeFormatter中提供好的日期格式常量
    now1.format(DateTimeFormatter.ISO_LOCAL_DATE_TIME);

  • 使用自定义的日期格式格式化字符串
    DateTimeFormatter timeFormat = DateTimeFormatter.ofPattern(“yyyy-MM-dd HH:mm:ss”);//自定义一个日期格式
    String time = now1.format(timeFormat);
    System.out.println(time);

  • 把一个日期字符串转成日期对象
    使用日期类中的parse方法传入一个日期字符串,传入对应的日期格式化类
    DateTimeFormatter dateFormat = DateTimeFormatter.ofPattern(“yyyy-MM-dd”);
    LocalDateTime parse = LocalDateTime.parse(time, timeFormat);
    System.out.println(parse);

ZonedDate,ZonedTime、ZonedDateTime : 带时区的时间或日期

  • 用法和 LocalDate、 LocalTime、 LocalDateTime 一样 只不过ZonedDate,ZonedTime、ZonedDateTime 这三个带有当前系统的默认时区

ZoneID 世界时区类

  • 1.获取世界各个地方的时区的集合 的方法getAvailableZoneIds()
    使用ZoneID中的静态方法getAvailableZoneIds();来获取
    例如:Set availableZoneIds = ZoneId.getAvailableZoneIds();
  • 2.获取系统默认时区的ID
    ZoneId zoneId = ZoneId.systemDefault(); //Asia/Shanghai
  • 3.获取带有时区的日期时间对象
    //创建日期对象
    LocalDateTime now = LocalDateTime.now();
    //获取不同国家的日期时间根据各个地区的时区ID名创建对象
    ZoneId timeID = ZoneId.of(“Asia/Shanghai”);
    //根据时区ID获取带有时区的日期时间对象
    ZonedDateTime time = now.atZone(timeID);
    System.out.println(time);
    //方式2 通过时区ID 获取日期对象
    LocalDateTime now2 = LocalDateTime.now(ZoneId.of(“Asia/Shanghai”));
    System.out.println(now2);
Logo

CSDN联合极客时间,共同打造面向开发者的精品内容学习社区,助力成长!

更多推荐