java8新特性之日期时间操作

1、常用API。

方法概述
now()根据当前时间创建对象
of()根据指定时间创建对象
plusDays()/ plusWeeks()/plusMonths()/plusYears()向当前 LocalDate 对象添加几天、几周、几月、几年
minusDays()/minusWeeks()/minusMonths()/minusYears()从当前 LocalDate 对象减去几天、几周、几月、几年
plus, minus添加或减少一个 Duration 或 Period
withDayOfMonth()/withDayOfYear()/withMonth()/withYear()将月份天数、年份天数、月份、年份 修 改 为 指 定 的 值 并 返 回 新 的LocalDate 对象
getDayOfMonth()/getDayOfYear()/getDayOfWeek()获得月份天数(1-31),获得年份天数(1-366),获得星期几(返回一个 DayOfWeek枚举值)
getMonth()/getMonthValue()/getYear()获得月份, 返回一个 Month 枚举值,获得月份(1-12),年份
until()获得两个日期之间的 Period 对象,或者指定 ChronoUnits 的数字
isBefore()/ isAfter()比较两个 LocalDate
isLeapYear()判断是否是闰年

2、获取日期或时间。

@Test
 public void test1() {
     //需求1:获取当前系日期或时间
     System.out.println(LocalDate.now());
     System.out.println(LocalTime.now());
     System.out.println(LocalDateTime.now());

     //需求2:获取指定日期或时间
     System.out.println(LocalDate.of(2015, 10, 15));
     System.out.println(LocalTime.of(9,58));
 }

3、Instant 时间戳。

  • 用于“时间戳”的运算。它是以Unix元年(传统的设定为UTC时区1970年1月1日午夜时分)开始所经历的描述进行运算。
@Test
public void test2() {
    //默认获取UTC 时区
    Instant ins1 = Instant.now();

    //添加8小时
    OffsetDateTime ofd = ins1.atOffset(ZoneOffset.ofHours(8));

    //转为时间戳
    System.out.println(ins1.toEpochMilli());

    //获取默认UTC 后多长时间
    Instant ins2 = Instant.ofEpochSecond(600);
    System.out.println(ins2);
}

4、Duration 和 Period。

  • Duration:用于计算两个“时间”间隔。
 @Test
 public void test3() {
     //计算两个时间戳直接的间隔
     Instant ins1 = Instant.now();
     try {
         Thread.sleep(1500);
     } catch (InterruptedException e) {
         e.printStackTrace();
     }

     Instant ins2 = Instant.now();

     Duration dur = Duration.between(ins1, ins2);
     System.out.println(dur.toMillis());

     //计算两个时间之间的间隔
     LocalDateTime ls1 = LocalDateTime.now();
     try {
         Thread.sleep(1000);
     } catch (InterruptedException e) {
         e.printStackTrace();
     }

     LocalDateTime ls2 = LocalDateTime.now();

     Duration dus = Duration.between(ls1, ls2);
     System.out.println(dus.toMillis());
 }
  • Period:用于计算两个“日期”间隔。
@Test
public void test4() {
    LocalDate ls1 = LocalDate.of(2013, 10, 21);
    LocalDate ls2 = LocalDate.now();
    Period per = Period.between(ls1, ls2);
    System.out.println(per.getYears() + "年" + per.getMonths() + "月" + per.getDays()+"天");
}

5、TemporalAdjuster : 时间校正器。

  • TemporalAdjusters : 该类通过静态方法提供了大量的常用 TemporalAdjuster 的实现。
@Test
public void test5() {
    LocalDateTime ls1 = LocalDateTime.now();
    System.out.println(ls1);

    //从当前时间减去 n天
    LocalDateTime ls2 = ls1.withDayOfMonth(10);
    System.out.println(ls2);

    //获取下一个周日
    LocalDateTime ls3 = ls1.with(TemporalAdjusters.next(DayOfWeek.SUNDAY));
    System.out.println(ls3);
}

6、DateTimeFormatter:解析与格式化。

@Test
public void test6() {
    //格式化为ISO指定类型的时间
    DateTimeFormatter dtf = DateTimeFormatter.ISO_DATE_TIME;
    LocalDateTime ld1 = LocalDateTime.now();
    System.out.println(ld1.format(dtf));

    //自定义格式化
    DateTimeFormatter dtf2 = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
    System.out.println(dtf2.format(ld1));
}

7、时区

  • ZoneId中包含了所支持的时区。
@Test
public void test7() {
    //获取所有支持的时区
    Set<String> set = ZoneId.getAvailableZoneIds();
    set.forEach(System.out::println);
}
  • ZonedDateTime:获取带时区的时间。
@Test
public void test9() {
    System.out.println(ZonedDateTime.now());
}
  • of(id) : 用指定的时区信息获取 ZoneId 对象。
 @Test
 public void test8() {
     //获取指定时区的时间
     LocalDateTime ldt = LocalDateTime.now(ZoneId.of("Europe/Zagreb"));
     System.out.println(ldt);
 }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值