加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

Java8中时间日期库的20个常用使用示例

发布时间:2020-12-15 03:13:22 所属栏目:Java 来源:网络整理
导读:今天PHP站长网 52php.cn把收集自互联网的代码分享给大家,仅供参考。 Instant——它代表的是时间戳 LocalDate——不包含具体时间的日期,比如2014-01-14。它可以用来存储生日,周年纪念日,入职日期等。 LocalTime——它

以下代码由PHP站长网 52php.cn收集自互联网

现在PHP站长网小编把它分享给大家,仅供参考

  • Instant——它代表的是时间戳
  • LocalDate——不包含具体时间的日期,比如2014-01-14。它可以用来存储生日,周年纪念日,入职日期等。
  • LocalTime——它代表的是不含日期的时间
  • LocalDateTime——它包含了日期及时间,不过还是没有偏移信息或者说时区。
  • ZonedDateTime——这是一个包含时区的完整的日期时间,偏移量是以UTC/格林威治时间为基准的。

Java 8是如何处理时间及日期的

示例1 如何 在Java 8中获取当天的日期

LocalDate today = LocalDate.now(); System.out.println("Today's Local date : " + today); 

Output 
Today's Local date : 2014-01-14

示例2 如何在Java 8中获取当前的年月日

LocalDate today = LocalDate.now(); 
int year = today.getYear(); 
int month = today.getMonthValue(); 
int day = today.getDayOfMonth(); 
System.out.printf("Year : %d Month : %d day : %d t %n",year,month,day); 

Output 
Today's Local date : 2014-01-14 
Year : 2014 Month : 1 day : 14

示例3 在Java 8中如何获取某个特定的日期

LocalDate dateOfBirth = LocalDate.of(2010,01,14); 
System.out.println("Your Date of birth is : " + dateOfBirth); 

Output : Your Date of birth is : 2010-01-14

示例4 在Java 8中如何检查两个日期是否相等

LocalDate date1 = LocalDate.of(2014,14); if(date1.equals(today)){ 
    System.out.printf("Today %s and date1 %s are same date %n",today,date1); 
} 

Output 
today 2014-01-14 and date1 2014-01-14 are same date

示例5 在Java 8中如何检查重复事件,比如说生日

LocalDate dateOfBirth = LocalDate.of(2010,14); 
MonthDay birthday = MonthDay.of(dateOfBirth.getMonth(),dateOfBirth.getDayOfMonth()); 
MonthDay currentMonthDay = MonthDay.from(today); 
if(currentMonthDay.equals(birthday)){ 
    System.out.println("Many Many happy returns of the day !!"); 
}else{ 
    System.out.println("Sorry,today is not your birthday"); 
} 

Output: Many Many happy returns of the day !!

示例6 如何在Java 8中获取当前时间

LocalTime time = LocalTime.now(); System.out.println("local time now : " + time);

Output 
local time now : 16:33:33.369 // in hour,minutes,seconds,nano seconds

示例7 如何增加时间里面的小时数

LocalTime time = LocalTime.now(); 
LocalTime newTime = time.plusHours(2); // adding two hours 
System.out.println("Time after 2 hours : " + newTime); 

Output : 
Time after 2 hours : 18:33:33.369

示例8 如何获取1周后的日期

LocalDate nextWeek = today.plus(1,ChronoUnit.WEEKS); 
System.out.println("Today is : " + today); 
System.out.println("Date after 1 week : " + nextWeek); 

Output: 
Today is : 2014-01-14 
Date after 1 week : 2014-01-21

示例9 一年前后的日期

LocalDate previousYear = today.minus(1,ChronoUnit.YEARS); 
System.out.println("Date before 1 year : " + previousYear); 
LocalDate nextYear = today.plus(1,YEARS); 
System.out.println("Date after 1 year : " + nextYear); 

Output: 
Date before 1 year : 2013-01-14 
Date after 1 year : 2015-01-14

示例10 在Java 8中使用时钟

// Returns the current time based on your system clock and set to UTC. 
Clock clock = Clock.systemUTC(); 
System.out.println("Clock : " + clock); 

// Returns time based on system clock zone Clock defaultClock = 
Clock.systemDefaultZone(); 
System.out.println("Clock : " + clock); 

Output: 
Clock : SystemClock[Z] 
Clock : SystemClock[Z]
public class MyClass { 
    private Clock clock; // dependency inject ... 

    public void process(LocalDate eventDate) { 

        if(eventDate.isBefore(LocalDate.now(clock)) { 
            ... 
        } 
    } 
}

示例11 在Java中如何判断某个日期是在另一个日期的前面还是后面

LocalDate tomorrow = LocalDate.of(2014,1,15); 、if(tommorow.isAfter(today)){ 
    System.out.println("Tomorrow comes after today"); 
} 
LocalDate yesterday = today.minus(1,DAYS); 
if(yesterday.isBefore(today)){ 
    System.out.println("Yesterday is day before today"); 
} 

Output: 
Tomorrow comes after today 
Yesterday is day before today

示例12 在Java 8中处理不同的时区

// Date and time with timezone in Java 8 ZoneId america = ZoneId.of("America/New_York"); 
LocalDateTime localtDateAndTime = LocalDateTime.now(); 
ZonedDateTime dateAndTimeInNewYork = ZonedDateTime.of(localtDateAndTime,america ); 
System.out.println("Current date and time in a particular timezone : " + dateAndTimeInNewYork); 

Output : 
Current date and time in a particular timezone : 2014-01-14T16:33:33.373-05:00[America/New_York]
Exception in thread "main" java.time.zone.ZoneRulesException: Unknown time-zone ID: ASIA/Tokyo
        at java.time.zone.ZoneRulesProvider.getProvider(ZoneRulesProvider.java:272)
        at java.time.zone.ZoneRulesProvider.getRules(ZoneRulesProvider.java:227)
        at java.time.ZoneRegion.ofId(ZoneRegion.java:120)
        at java.time.ZoneId.of(ZoneId.java:403)
        at java.time.ZoneId.of(ZoneId.java:351)

示例13 如何表示固定的日期,比如信用卡过期时间

YearMonth currentYearMonth = YearMonth.now(); System.out.printf("Days in month year %s: %d%n",currentYearMonth,currentYearMonth.lengthOfMonth()); 
YearMonth creditCardExpiry = YearMonth.of(2018,Month.FEBRUARY); 
System.out.printf("Your credit card expires on %s %n",creditCardExpiry); 

Output: 
Days in month year 2014-01: 31 
Your credit card expires on 2018-02

示例14 如何在Java 8中检查闰年

if(today.isLeapYear()){ 
    System.out.println("This year is Leap year"); 
}else { 
    System.out.println("2014 is not a Leap year"); 
} 

Output: 2014 is not a Leap year

示例15 两个日期之间包含多少天,多少个月

LocalDate java8Release = LocalDate.of(2014,Month.MARCH,14); 
Period periodToNextJavaRelease = 
Period.between(today,java8Release); 
System.out.println("Months left between today and Java 8 release : " + periodToNextJavaRelease.getMonths() ); 

Output: 
Months left between today and Java 8 release : 2

示例16 带时区偏移量的日期与时间

LocalDateTime datetime = LocalDateTime.of(2014,Month.JANUARY,14,19,30); 
ZoneOffset offset = ZoneOffset.of("+05:30"); 
OffsetDateTime date = OffsetDateTime.of(datetime,offset); 
System.out.println("Date and Time with timezone offset in Java : " + date); 

Output : 
Date and Time with timezone offset in Java : 2014-01-14T19:30+05:30

示例17 在Java 8中如何获取当前时间戳

Instant timestamp = Instant.now(); 
System.out.println("What is value of this instant " + timestamp); 

Output : 
What is value of this instant 2014-01-14T08:33:33.379Z

示例18 如何在Java 8中使用预定义的格式器来对日期进行解析/格式化

String dayAfterTommorrow = "20140116"; 
LocalDate formatted = LocalDate.parse(dayAfterTommorrow,DateTimeFormatter.BASIC_ISO_DATE); 
System.out.printf("Date generated from String %s is %s %n",dayAfterTommorrow,formatted); 

Output : 
Date generated from String 20140116 is 2014-01-16

示例19 如何在Java中使用自定义的格式器来解析日期

String goodFriday = "Apr 18 2014"; 
try { 
    DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MMM dd yyyy");     
    LocalDate holiday = LocalDate.parse(goodFriday,formatter); 
    System.out.printf("Successfully parsed String %s,date is %s%n",goodFriday,holiday); 
} catch (DateTimeParseException ex) { 
    System.out.printf("%s is not parsable!%n",goodFriday); 
    ex.printStackTrace(); 
} 

Output : 
Successfully parsed String Apr 18 2014,date is 2014-04-18

示例20 如何在Java 8中对日期进行格式化,转换成字符串

LocalDateTime arrivalDate = LocalDateTime.now(); 
try { 
    DateTimeFormatter format = DateTimeFormatter.ofPattern("MMM dd yyyy hh:mm a"); 
    String landing = arrivalDate.format(format); 
    System.out.printf("Arriving at : %s %n",landing); 
    } catch (DateTimeException ex) { 
    System.out.printf("%s can't be formatted!%n",arrivalDate); 
    ex.printStackTrace(); 
} 

Output : Arriving at : Jan 14 2014 04:33 PM

Java 8中日期与时间API的几个关键点

  1. 它提供了javax.time.ZoneId用来处理时区。
  2. 它提供了LocalDate与LocalTime类
  3. Java 8中新的时间与日期API中的所有类都是不可变且线程安全的,这与之前的Date与Calendar API中的恰好相反,那里面像java.util.Date以及SimpleDateFormat这些关键的类都不是线程安全的。
  4. 新的时间与日期API中很重要的一点是它定义清楚了基本的时间与日期的概念,比方说,瞬时时间,持续时间,日期,时间,时区以及时间段。它们都是基于ISO日历体系的。
  5. 每个Java开发人员都应该至少了解这套新的API中的这五个类:
    • Instant 它代表的是时间戳,比如2014-01-14T02:20:13.592Z,这可以从java.time.Clock类中获取,像这样: Instant current = Clock.system(ZoneId.of(“Asia/Tokyo”)).instant();
    • LocalDate 它表示的是不带时间的日期,比如2014-01-14。它可以用来存储生日,周年纪念日,入职日期等。
    • LocalTime – 它表示的是不带日期的时间
    • LocalDateTime – 它包含了时间与日期,不过没有带时区的偏移量
    • ZonedDateTime – 这是一个带时区的完整时间,它根据UTC/格林威治时间来进行时区调整
  6. 这个库的主包是java.time,里面包含了代表日期,时间,瞬时以及持续时间的类。它有两个子package,一个是java.time.foramt,这个是什么用途就很明显了,还有一个是java.time.temporal,它能从更低层面对各个字段进行访问。
  7. 时区指的是地球上共享同一标准时间的地区。每个时区都有一个唯一标识符,同时还有一个地区/城市(Asia/Tokyo)的格式以及从格林威治时间开始的一个偏移时间。比如说,东京的偏移时间就是+09:00。
  8. OffsetDateTime类实际上包含了LocalDateTime与ZoneOffset。它用来表示一个包含格林威治时间偏移量(+/-小时:分,比如+06:00或者 -08:00)的完整的日期(年月日)及时间(时分秒,纳秒)。
  9. DateTimeFormatter类用于在Java中进行日期的格式化与解析。与SimpleDateFormat不同,它是不可变且线程安全的,如果需要的话,可以赋值给一个静态变量。DateTimeFormatter类提供了许多预定义的格式器,你也可以自定义自己想要的格式。当然了,根据约定,它还有一个parse()方法是用于将字符串转换成日期的,如果转换期间出现任何错误,它会抛出DateTimeParseException异常。类似的,DateFormatter类也有一个用于格式化日期的format()方法,它出错的话则会抛出DateTimeException异常。
  10. 再说一句,“MMM d yyyy”与“MMm dd yyyy”这两个日期格式也略有不同,前者能识别出”Jan 2 2014″与”Jan 14 2014″这两个串,而后者如果传进来的是”Jan 2 2014″则会报错,因为它期望月份处传进来的是两个字符。为了解决这个问题,在天为个位数的情况下,你得在前面补0,比如”Jan 2 2014″应该改为”Jan 02 2014″。

以上内容由PHP站长网【52php.cn】收集整理供大家参考研究

如果以上内容对您有帮助,欢迎收藏、点赞、推荐、分享。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读