java常用API-JDK8新时间

141 阅读6分钟

java常用API-JDK8新时间

Java 8 吸收了 java-Time 的精华,以一个新的开始为 Java 创建优秀的 API。新的 java.time 中包含了所有关于本地日期(LocalDate)、本地时间(LocalTime)、本地日期时间(LocalDateTime)、时(ZonedDateTime)和持续时间(Duration)的类等等。这些新增的本地化时间日期 API 大大简化了日期时间和本地化的管理。

date1.png 主要就是有这些新增的替代了原先的

相比较与之前的时间:

  1. 设计更加合理、功能丰富、使用更加方便
  2. 都是不可变的对象,修改后返回新的时间对象,不会丢失最开始的时机
  3. 线程安全
  4. 能精确到毫秒、纳秒

LocalDate、LocalTime、LocalDateTime

LocalDate:代表本地日期(年、月、日)

LocalTime:代表本地时间(时、分、秒、纳秒)

LocalDateTime:代表本地日期、时间(年、月、日、星期、时、分、秒、纳秒)

方法名示例
public static Xxxxnow(获取系统当前时间对应的该对象)LocaDate ld = LocalDate.now(); LocalTime lt = LocalTime.now(); LocalDateTime ldt = LocalDateTime.now();

localDate常用API(都是处理年、月、日、星期相关的)

方法名说明
public int geYear()获取年
public int getMonthvalue()获取月份(1-12)
public int getDayOfMonth()获取日
public int getDay0fYear()获取当前是一年中的第几天
Public DayOfweek getDayOfweek()获取星期几: ld.getDayOfweek().getValue()
方法名说明
withYear、withMonth、withDayOfMonth、withDayOfYear直接修改某个信息,返回新日期对象
plusYears、plusMonths、plusDays、plusWeeks把某个信息加多少,返回新日期对象
minusYears、minusMonths、minusDays , minusWeeks把某个信息减多少,返回新日期对象
equals isBefore isAfter判断两个日期对象,是否相等,在前还是在后

如果想要设置一个自定义的日期可以用of方法:

设置时间,日期和时间都可以用这个of方法

LocalDate date11111 = LocalDate.of(2003, 9, 23);
System.out.println(date11111); //2003-09-23

LocalTime的常用API(都是处理时、分、秒、纳秒相关的)。

方法名说明
public int getHour()获取小时
public int getMinute()获取分
public int getSecond()获取秒
public int getNano()获取纳秒
withHour、withMinute、withSecond、withNano修改时间,返回新时间对象
plusHours、plusMinutes、plusSeconds、plusNanos把某个信息加多少,返回新时间对象
minusHours、minusMinutes、minusSeconds、minusNanos把某个信息减多少,返回新时间对象
equals isBefore isAfter判断2个时间对象,是否相等,在前还是在后

LocalDateTime的常用API(可以处理年、月、日、星期、时、分、秒、纳秒

方法名说明
getYear.getMonthValue、getDayOfMonth、getDayOfYear获取年月日、时分秒、纳秒等
getDayOfweek、getHour、getMinute、getSecond、getNano获取年月日、时分秒、纳秒等
withYear、withMonth、withDayOfMonth、withDayofYear修改某个信息,返回新日期时间对象
withHour、withMinute、withSecond、withNano修改某个信息,返回新日期时间对象
plusYears、plusMonths、plusDays、plusweeks把某个信息加多少,返回新日期时间对象
plusHours、plusMinutes、plusSeconds、plusNanos把某个信息加多少,返回新日期时间对象
minusYears、minusMonths、minusDays、minusweeks把某个信息减多少,返回新日期时间对象
minusHours、minusMinutes、minusSeconds、minusNanos把某个信息减多少,返回新日期时间对象
equals isBefore isAfter把某个信息减多少,返回新日期时间对象
System.out.println("=====JDK8的新时间====");
//localDate  localTime  LocalDateTime
LocalDate localDate = LocalDate.now();
System.out.println(localDate);
LocalTime localTime = LocalTime.now();
System.out.println(localTime);
LocalDateTime localDateTime = LocalDateTime.now();
System.out.println(localDateTime);
​
//of()设置指定的年,月,日,时,分,秒,没有偏移量
LocalDateTime localDateTime1 = LocalDateTime.of(2022, 3, 18, 2, 57, 40);
System.out.println(localDateTime1);
​
//getXxx()
System.out.println(localDateTime.getDayOfMonth());
System.out.println(localDateTime.getDayOfWeek());
System.out.println(localDateTime.getDayOfYear());
System.out.println(localDateTime.getMonthValue());
​
//体现不可变性
//withXxx():设置相关属性
LocalDate localDate1 = localDate.withDayOfMonth(22);
System.out.println(localDate1);
System.out.println(localDate);
​
LocalDateTime localDateTime2 = localDateTime.withHour(4);
System.out.println(localDateTime);
System.out.println(localDateTime2);
​
//不可变性
LocalDateTime localDateTime4 = localDateTime.plusMonths(3);
System.out.println(localDateTime);
System.out.println(localDateTime4);

ZoneId 时区、ZoneDateTime 带时区的时间

什么是时区?

由于世界各个国家与地区的进度不同,各地区的时间也有所不同,因此会划分为不同的时区

洲名/城市名 Asia/Shanghai

国家名/城市名 America/New_York Zoneld时区的常见方法

方法名
public static Set getAvailableZonelds()获取Java中支持的所有时区
public static Zoneld systemDefault()获取系统默认时区
public static Zoneld of(String zoneld)获取一个指定时区

ZonedDateTime带时区时间的常见方法

方法名说明
public static ZonedDateTime now()获取当前时区的ZonedDateTime对象
public static ZonedDateTime now(Zoneld zone)获取指定时区的ZonedDateTime对象
getYear、getMonthValue.getDayOfMonth、getDayOfYeargetDayOfWeek、getHour、getMinute、getSecond、getNano获取年月日、时分秒、纳秒等
public ZonedDateTime withXxx(时间)修改时间系列的方法
public ZonedDateTime minusXxx(时间)减少时间系列的方法
public ZonedDateTime plusXxx(时间)增加时间系列的方法

基本使用:

//获取带有时区的时间
ZonedDateTime now = ZonedDateTime.now();
System.out.println(now);
//获取时区
Set<String> availableZoneIds = ZoneId.getAvailableZoneIds();
System.out.println(availableZoneIds);
ZoneId zoneId = ZoneId.systemDefault();
System.out.println(zoneId);
//设置时区
ZoneId zoneId1 = ZoneId.of("Africa/Nairobi");
System.out.println(zoneId1);

Instant 时间线上的某个时刻/时间戳

通过获取Instant的对象可以拿到此刻的时间,该时间由两部分组成:从1970-01-0100:00:00开始走到此刻的总秒数+不够1秒的纳秒数

1秒=1000毫秒;

1毫秒=1000微秒;

1微秒=1000纳秒;

1秒=1000 000 000 纳秒

方法名说明
public static Instant now()获取当前时间的Instant对象(标准时间)
public long getEpochSecond()获取从1970-01-01T00: 00:00开始记录的秒数。
public int getNano()获取从197日-e1-01T00: 00:00开始记录的秒数。
plusMillis plusseconds plusNanos增加时间系列的方法
minusMillis minusSeconds minusNanos减少时间系列的方法
equals、isBefore、isAfter判断系列的方法

作用:可以用来记录代码的执行时间,或用于记录用户操作某个事件的时间点。

传统的Date类,只能精确到毫秒,并且是可变对象;

新增的Instant类,可以精确到纳秒,并且是不可变对象,推荐用Instant代替Date。

//时间戳 instant
Instant now1 = Instant.now();
System.out.println(now1);
//获取毫秒值
long epochSecond = now1.getEpochSecond();
System.out.println(epochSecond);
//获取纳秒值
long nano = now1.getNano();
System.out.println(nano);

DateTimeFormatter 格式化

和简单格式化SimpleDateformat 很像

方法名说明
public static DateTimeFormatter ofPattern(时间格式)获取格式化器对象
public string format(时间对象)格式化时间
//时间格式化
​
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
String format = dateTimeFormatter.format(localDateTime);
System.out.println(format);

解析时间一般用LocalDateTime 的parse来解析

//解析时间一般是用localDateTime 的 parse 来解析
String date11="2002-9-10 12:12:12";
LocalDateTime parse = LocalDateTime.parse(date11, dateTimeFormatter);
System.out.println(parse);

Period 计算时间间隔(年月日) Duration 计算时间间隔(时分秒纳秒)

Period(一段时期)

可以用于计算两个LocalDate对象相差的年数、月数、天数。

方法名说明
public static Period between(LocalDate start,LocalDate end)传入2个日期对象,得到Period对象
public int getYears()计算隔几年,并返回
public int getMonths()计算隔几个月,并返回
public int getDays()计算隔多少天,并返回
System.out.println("=================");
LocalDate now2 = LocalDate.now();
LocalDate localDate2 = now2.withYear(2002).withMonth(10).withDayOfMonth(15);
System.out.println(localDate2);
LocalDate now3= LocalDate.now();
LocalDate localDate23= now3.withYear(2006).withMonth(12).withDayOfMonth(10);
System.out.println(localDate23);
​
//计算时间间隔
Period between = Period.between(localDate2, localDate23);
int years = between.getYears();
int months = between.getMonths();
int days = between.getDays();
System.out.println(years); // 4
System.out.println(months);//1
System.out.println(days); //25

Duration(持续时间)

可以用于计算两个时间对象相差的天数、小时数、分数、秒数、纳秒数;支持LocalTime、LocalDateTime、Instant等时间

方法名说明
public static Duration between(开始时间对象1,截止时间对象2)传入2个时间对象,得到Duration对象
public long toDays()计算隔多少天,并返回
public long toHours()计算隔多少小时,并返回
public long toMinutes()计算隔多少分,并返回
public long toSeconds()计算隔多少秒,并返回
public long toMillis()计算隔多少毫秒,并返回
public long toNanos()计算隔多少纳秒,并返回

用法和Period一样

可以更加方便的计算出两个时间点的相差多少