Java8实战之新时间日期API

简介

Java 8通过发布新的Date-Time API (JSR 310)来进一步加强对日期与时间的处理。

在旧版的 Java 中,日期时间 API 存在诸多问题,其中有:

非线程安全: java.util.Date 是非线程安全的,所有的日期类都是可变的,这是Java日期类最大的问题之一。

设计很差:Java的日期/时间类的定义并不一致,在java.util和java.sql的包中都有日期类,此外用于格式化和解析的类在java.text包中定义。java.util.Date同时包含日期和时间,而java.sql.Date仅包含日期,将其纳入java.sql包并不合理。另外这两个类都有相同的名字,这本身就是一个非常糟糕的设计。

时区处理麻烦:日期类并不提供国际化,没有时区支持,因此Java引入了java.util.Calendar和java.util.TimeZone类,但他们同样存在上述所有的问题。

Java 8 在 java.time 包下提供了很多新的 API。以下为两个比较重要的 API:

Local(本地): 简化了日期时间的处理,没有时区的问题。

Zoned(时区):通过制定的时区处理日期时间。

新的java.time包涵盖了所有处理日期,时间,日期/时间,时区,时刻(instants),过程(during)与时钟(clock)的操作

示例

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

@Test
public void test1(){
    // LocalDate 当前日期
    LocalDate localDate = LocalDate.now();
    // LocalTime 当前时间
    LocalTime localTime = LocalTime.now();
    // LocalDateTime当前年月日时分秒
    LocalDateTime localDateTime = LocalDateTime.now();
}

常用方法介绍:
of():静态方法,根据指定日期/时间创建对象

@Test
public void test2(){
    LocalDate localDate = LocalDate.of(2019, 8, 17);
    LocalTime localTime = LocalTime.of(22, 22, 56);
    LocalDateTime localDateTime = LocalDateTime.of(2019, 8, 17, 22, 22, 56);
}

plusDays,plusWeeks,plusMonths,plusYears:向当前LocalDate对象添加几天、几周、几个月、几年

@Test
public void test3(){
    LocalDateTime localDateTime = LocalDateTime.now();

    localDateTime.plusDays(1);
    localDateTime.plusWeeks(1);
    localDateTime.plusMonths(1);
    localDateTime.plusYears(1);
}

minusDays,minusWeeks,minusMonths,minusYears:从当前LocalDate对象减去几天、几周、几个月、几年

@Test
public void test4(){

    LocalDateTime localDateTime = LocalDateTime.now();

    localDateTime.minusDays(1);
    localDateTime.minusWeeks(1);
    localDateTime.minusMonths(1);
    localDateTime.minusYears(1);
}

withDayOfMonth,withDayOfYear,withMonth,withYear:将月份天数、年份天数、月份、年份修改为指定的值并返回新的LocalDate对象

@Test
public void test5(){
     LocalDateTime localDateTime = LocalDateTime.now();

     localDateTime.withDayOfMonth(1);
     localDateTime.withDayOfYear(6);
     localDateTime.withMonth(3);
     localDateTime.withYear(2018);
 }

getDayOfMonth:获得月份天数(1-31)

@Test
public void test6(){
    LocalDate localDate = LocalDate.now();

    localDate.getDayOfMonth();
}

getDayOfYear:获得年份天数(1-366)

@Test
public void test7(){
   LocalDate localDate = LocalDate.now();

   localDate.getDayOfYear();
}

getDayOfWeek:获得星期几(返回一个DayOfWeek枚举值)

@Test
public void test8(){
    LocalDate localDate = LocalDate.now();

    localDate.getDayOfWeek();
}

getMonth:获得月份,返回一个Month枚举值

@Test
public void test9(){
    LocalDate localDate = LocalDate.now();

    Month month = localDate.getMonth();
}

getMonthValue :获得月份(1-12)

@Test
public void test10(){
    LocalDate localDate = LocalDate.now();

    localDate.getMonthValue();
}

getYear:获得年份

@Test
public void test11(){
    LocalDate localDate = LocalDate.now();

    localDate.getYear();
}

Duration: 计算两个时间的间隔
LocalDate:计算两个日期之间的间隔

@Test
public void test12(){
    LocalDateTime d1 = LocalDateTime.of(2018,10,10,12,12,30);
    LocalDateTime now = LocalDateTime.now();

    Duration between = Duration.between(d1, now);
    System.out.println(between.getSeconds());


    LocalDate d2 = LocalDate.of(2018,10,10);
    Period period = Period.between(LocalDate.now(), d2);
    System.out.println(period.getYears());
    System.out.println(period.getMonths());
    System.out.println(period.getDays());

}

isBefore,isAfter:比较两个LocalDate

@Test
public void test13(){
    LocalDate localDate = LocalDate.now();

    localDate.isAfter(LocalDate.now());
    localDate.isBefore(LocalDate.now());
}

isLeapYear:判断是否是闰年

@Test
public void test14(){
    LocalDate localDate = LocalDate.now();
    localDate.isLeapYear();
}

Instant:时间戳

@Test
public void test15(){
    Instant now = Instant.now(); // 默认获取 UTC 时区
    System.out.println(now);

    OffsetDateTime offsetDateTime = now.atOffset(ZoneOffset.ofHours(8)); // 偏移量
    System.out.println(offsetDateTime);

    System.out.println(now.toEpochMilli()); // 时间戳
}

TemporalAdjuster:时间矫正器

@Test
public void test16(){
   LocalDateTime now = LocalDateTime.now();
   System.out.println(now);


   LocalDateTime localDateTime = now.withDayOfMonth(25);
   System.out.println(localDateTime);

   // 下一个星期一
   LocalDateTime with = now.with(TemporalAdjusters.next(DayOfWeek.MONDAY));
   System.out.println(with);


   // 自定义,下一个工作日
   LocalDateTime with1 = now.with((e) -> {
       LocalDateTime localDateTime1 = (LocalDateTime) e;
       DayOfWeek dayOfWeek = localDateTime1.getDayOfWeek();
       if (dayOfWeek.equals(DayOfWeek.FRIDAY)) {
           return localDateTime1.plusDays(3);
       } else if (dayOfWeek.equals(DayOfWeek.SATURDAY)) {
           return localDateTime1.plusDays(2);
       } else {
           return localDateTime1.plusDays(1);
       }
   });

   System.out.println(with1);
}

DateTimeFormatter:格式化日期时间

@Test
public void test17(){

    DateTimeFormatter isoDateTime = DateTimeFormatter.ISO_LOCAL_DATE_TIME;

    LocalDateTime localDateTime = LocalDateTime.now();
    System.out.println(localDateTime.format(isoDateTime));

    // 格式化
    DateTimeFormatter yyyyMMdd = DateTimeFormatter.ofPattern("yyyyMMdd");
    String format = localDateTime.format(yyyyMMdd);
    System.out.println(format);

    // 反格式化
    LocalDate parse = LocalDate.parse(format, yyyyMMdd);
    System.out.println(parse);
}

Zone:时区处理

@Test
public void test18(){
   // 支持的时区
   Set<String> availableZoneIds = ZoneId.getAvailableZoneIds();
   availableZoneIds.forEach(System.out::println);

   LocalDateTime now = LocalDateTime.now(ZoneId.of("Asia/Shanghai"));
   System.out.println(now);

   ZonedDateTime zonedDateTime = now.atZone(ZoneId.of("Asia/Shanghai"));
   System.out.println(zonedDateTime);
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值