新时间API

本文介绍了Java8中的日期和时间API,包括LocalDate,LocalTime,LocalDateTime的使用,如获取当前时间、日期转换、时间间隔计算等操作,以及Instant类用于处理时间戳的方法。
摘要由CSDN通过智能技术生成

本地化日期时间API:

LocalDate LocalTime LocalDateTime

LocalDate的方法:now() , of() , ofYearDay() , parse() , range() , plusDays() , minusDays() , until() , format(DateTimeFormatter.ofPattern) ,

//TODO 获取当前时间

LocalDate now = LocalDate.now();

System.out.println(now);

//TODO 获取2019年12月21日 时间对象

LocalDate of = LocalDate.of(2019, 12, 21);

System.out.println(of);

//TODO 获取2017年第223天的日期

LocalDate localDate = LocalDate.ofYearDay(2017, 223);

System.out.println(localDate);

//将标准的yyyy-MM-dd 字符串转化为日期格式

LocalDate parse = LocalDate.parse("2019-02-12");

System.out.println(parse);

//查看parse日期的2月份天的范围

ValueRange range = parse.range(ChronoField.DAY_OF_MONTH);

System.out.println(range);

//查看parse年的天数

ValueRange range1 = parse.range(ChronoField.DAY_OF_YEAR);

System.out.println(range1);

//查看pares的后20天的日期

LocalDate localDate1 = parse.plusDays(20);

System.out.println(localDate1);

//查看parse的前15天的日期

LocalDate localDate2 = parse.minusDays(15);

System.out.println(localDate2);

//查看localDate1 和 localDate2 之间相差的天数

Period until = localDate2.until(localDate1);

System.out.println(until);

//将localDate2的日期转化为 day/month/year

String format= localDate2.format(

DateTimeFormatter.ofPattern("dd//MM/yyyy")

);

System.out.println(format);

}

LocalTime的核心方法:now() , plusHours() , isAfter() , minusHours() , isBefore() , format()

LocalTime now2 = LocalTime.now();

System.out.println(now2);

// 获取伦敦的时间

LocalTime now3 = LocalTime.now(ZoneId.of("Europe/London"));

// 10个小时20分钟之后是几点

LocalTime localTime = now2.plusHours(10).plusMinutes(20);

System.out.println(localTime);

// 判断 delayTime时间是否为当前时间之后的时间

System.out.println(localTime.isAfter(now2));

// 当前时间的前20个小时间是几点

LocalTime localTime1 = now2.minusHours(20);

System.out.println(localTime1);

// 判断beforeTime是否在当前时间之前

System.out.println(localTime.isBefore(now2));

// 将当前时间转化为 hh:mm:ss 格式的字符串

String format1 = now2.format(DateTimeFormatter.ofPattern("hh:mm:ss"));

System.out.println(format1);

LocalDateTime的核心方法:now() , of() , format() , range.getMaximum() , range.getMinmum()

LocalDateTime now = LocalDateTime.now();

System.out.println(now);

//转化日期格式

String format = now.format(DateTimeFormatter.ofPattern("yyyy-MM-dd hh:mm:ss "));

System.out.println(format);

//查看now日期时间得所在月份的天范围

ValueRange range = now.range(ChronoField.DAY_OF_MONTH);

System.out.println(range.getMinimum()+"-"+range.getMaximum());

//获取第一天和最后一天的时间

LocalDate max = LocalDate.of(now.getYear(), now.getMonth(), (int) range.getMaximum());

LocalDate min = LocalDate.of(now.getYear(), now.getMonth(), (int) range.getMinimum());

System.out.println(min+"-"+max);

Instant获取时间戳:now() , atZone()

//获取时间戳

Instant now1 = Instant.now();

System.out.println(now1.toEpochMilli());

//获取时间戳 更改时区

Instant instant = Instant.now().atZone(ZoneId.of("Asia/Shanghai")).toInstant();

//查看时间戳的毫秒值

System.out.println(instant.toEpochMilli());

System.out.println(System.currentTimeMillis());

System.out.println(new Date().getTime()+","+new Date());

相互转换 :Date->Instant->LocalDateTime

Date date = new Date();

Instant instant1 = date.toInstant();

LocalDateTime localDateTime = LocalDateTime.ofInstant(instant1, ZoneId.systemDefault());

相互转化: LocalDateTime->Instant->Date

Date from = Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant());

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值