linux localtime 线程安全,LocalDate、LocalTime、LocalDateTime常用方法介绍(线程安全)...

一、JDK Release Notes:

https://www.oracle.com/technetwork/java/javase/jdk-relnotes-index-2162236.html?ssSourceSiteId=otncn

https://docs.oracle.com/javase/9/docs/api/java/time/package-summary.html

二、API Specification(API 规范)

包介绍:

java.time - Classes for date, time, date and time combined, time zones, instants, duration, and clocks.

java.time.chrono - API for representing calendar systems other than ISO-8601. Several predefined chronologies are provided and you can also define your own chronology.

java.time.format - Classes for formatting and parsing dates and time.

java.time.temporal - Extended API, primarily for framework and library writers, allowing interoperations between the date and time classes, querying, and adjustment. Fields and units are defined in this package.

java.time.zone - Classes that support time zones, offsets from time zones, and time zone rules.

类介绍:

Clock

A clock providing access to the current instant, date and time using a time-zone.

LocalDate

A date without a time-zone in the ISO-8601 calendar system, such as 2007-12-03.

LocalDateTime

A date-time without a time-zone in the ISO-8601 calendar system, such as 2007-12-03T10:15:30.

LocalTime

A time without a time-zone in the ISO-8601 calendar system, such as 10:15:30.

ZonedDateTime  (带时区的时间,不常用)

A date-time with a time-zone in the ISO-8601 calendar system, such as 2007-12-03T10:15:30+01:00 Europe/Paris.

DateTimeException

三、LocalDate、LocalTime、LocalDateTime常用方法介绍(线程安全)

1)可理解为 LocalDateTime = LocalDate + LocalTime

2)格式不正确可能导致类型转换处理异常

3)简单介绍基本类的常用情况、其他可自行扩展

1、LocalDate: 返回日期格式,如2019-01-01

//返回当前日期

System.out.println(LocalDate.now());

//返回指定日期(注意字段取值范围如月份取值1-12)

System.out.println(LocalDate.of(2019, 11, 30));

//返回一年种的第几天是多少号

System.out.println(LocalDate.ofYearDay(2019, 12));

//String and LocalDate 转换  (注意时间格式,其他格式导致异常)

System.out.println(LocalDate.parse("2017-12-03"));

System.out.println(LocalDate.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd")));

//日期加减计算

LocalDate localDate = LocalDate.now();

System.out.println(localDate.plusDays(1));

System.out.println(localDate.minusDays(3));

//单独取年、月、日方法

System.out.println(localDate.getYear() + "年" + localDate.getMonth().getValue() + "月" + localDate.getDayOfMonth() + "日");

2、LocalTime: 返回格式,如10:15:30

//当前时间 格式10:57:11.551

LocalTime localTime = LocalTime.now();

System.out.println(localTime);

System.out.println(localTime.withNano(0));

//返回指定的时间和分钟,秒和纳秒;不设置默认0

System.out.println(LocalTime.of(1, 1));

System.out.println(LocalTime.of(1, 1, 1));

System.out.println(LocalTime.of(1, 1, 1, 999));

//每天的第几秒开始、可用于计时器,参考示例如下

System.out.println(LocalTime.ofSecondOfDay(1));//00:00:01

System.out.println(LocalTime.ofSecondOfDay(60));//00:01

//替换指定时间单位上的值

System.out.println(localTime.withHour(1));

System.out.println(localTime.withMinute(2));

System.out.println(localTime.withSecond(3).withNano(1));

//时间的加减操作

System.out.println(localTime.plusHours(1));

System.out.println(localTime.plusMinutes(2));

System.out.println(localTime.plusSeconds(3));

System.out.println(localTime.plusNanos(100));

//其他同上

System.out.println(localTime.minusHours(1));

//LocalTime and Str 转换(注意格式化不可添加年月日)

System.out.println(localTime.format(DateTimeFormatter.ofPattern("hh:mm:ss")));

System.out.println(LocalTime.parse("10:01:01"));

//LocalDate to LocalDateTime(注意格式) 2019-03-04T15:42:22.233

System.out.println(localTime.atDate(LocalDate.now()));

//时间比较,返回int值

System.out.println(localTime.compareTo(localTime));

//时间比较,返回boolean值(注意格式和单位)

System.out.println("当前时间比较,注意纳秒不同:" + localTime + "||" + LocalTime.now());

System.out.println(localTime.isBefore(LocalTime.now()) + "||" + localTime.isAfter(LocalTime.now()));

System.out.println(localTime.withNano(0).isBefore(LocalTime.now().withNano(0)) + "||" + localTime.withNano(0).isAfter(LocalTime.now().withNano(0)));

System.out.println(localTime.equals(LocalTime.now()) + "||" + localTime.withNano(0).equals(LocalTime.now().withNano(0)));

3、LocalDateTime 返回格式,如2019-03-04T15:48:29.254

//基本使用及时间比较

LocalDateTime localDateTime = LocalDateTime.now();

System.out.println(localDateTime);

System.out.println(localDateTime.withNano(0));

System.out.println(localDateTime.compareTo(LocalDateTime.now()));

System.out.println(localDateTime.isAfter(localDateTime));

System.out.println(localDateTime.isBefore(localDateTime));

System.out.println(localDateTime.isEqual(localDateTime));

//LocalDateTime and Str 转换

System.out.println(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss").format(LocalDateTime.now()));

System.out.println(LocalDateTime.parse("2018-01-12 17:07:05", DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));

System.out.println(LocalDateTime.parse("2019-03-04T15:48:29.254").format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值