日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > java >内容正文

java

Java 8 - 时间API

發布時間:2025/3/21 java 23 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Java 8 - 时间API 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

文章目錄

  • Pre
  • 模擬SimpleDateFormate線程安全問題
  • LocalDate
  • LocalTime
  • LocalDateTime
  • Instant
  • Period
  • Duration
  • format
  • parse

Pre

并發編程-12線程安全策略之常見的線程不安全類


模擬SimpleDateFormate線程安全問題

package com.artisan.java8.testDate;import java.text.ParseException; import java.text.SimpleDateFormat; import java.time.*; import java.time.format.DateTimeFormatter; import java.time.temporal.ChronoField; import java.util.Date;/*** @author 小工匠* @version 1.0* @description: TODO* @date 2021/3/5 0:22* @mark: show me the code , change the world*/ public class DateInJava8 {public static void main(String[] args) throws ParseException, InterruptedException {Date date = new Date();System.out.println(date);/*** 模擬SimpleDateFormat 的線程安全問題** Exception in thread "Thread-30" java.lang.NumberFormatException: multiple points** Exception in thread "Thread-24" java.lang.NumberFormatException: For input string: "E.250214E4"**/SimpleDateFormat sdf = new SimpleDateFormat("YYYY-MM-dd");for (int i = 0; i < 100; i++) {new Thread(() -> {for (int j = 0; j < 20; j++) {try {String format = sdf.format(date);Date d = sdf.parse(format);System.out.println(d);} catch (ParseException e) {e.printStackTrace();}}}).start();}}}


LocalDate

https://nowjava.com/docs/java-api-11/java.base/java/time/LocalDate.html

LocalDate 是final修飾的不可變類 并且是線程安全的.

  • Java LocalDate is immutable class and hence thread safe.

  • LocalDate provides date output in the format of YYYY-MM-dd.

  • The LocalDate class has no time or Timezone data. So LocalDate is suitable to represent dates such as Birthday, National Holiday etc.

  • LocalDate class implements Temporal, TemporalAdjuster, ChronoLocalDate and Serializable interfaces.

  • LocalDate is a final class, so we can’t extend it.

  • LocalDate is a value based class, so we should use equals() method for comparing if two LocalDate instances are equal or not.

public static void testLocalDate(){LocalDate x = LocalDate.now(); // 日期 2021-03-05System.out.println(x);LocalDate lo = LocalDate.of(2021,3,5);System.out.println(lo.getYear()); // 年 2021System.out.println(lo.getMonth()); // 月 MARCHSystem.out.println(lo.getDayOfMonth()); // 日 5System.out.println(lo.getDayOfYear()); // 一年中的第幾天System.out.println(lo.getDayOfMonth());// 一個月中的第幾天System.out.println(lo.getDayOfWeek());// 禮拜幾System.out.println(lo.get(ChronoField.DAY_OF_MONTH));}

LocalTime

  • LocalTime provides time without any time zone information. It is very much similar to observing the time from a wall clock which just shown the time and not the time zone information.

  • The assumption from this API is that all the calendar system uses the same way of representing the time.

  • It is a value-based class, so use of reference equality (==), identity hash code or synchronization on instances of LocalTime may have - unexpected results and is highly advised to be avoided. The equals method should be used for comparisons.

  • The LocalTime class is immutable that mean any operation on the object would result in a new instance of LocalTime reference.

private static void testLocalTime() {LocalTime time = LocalTime.now();System.out.println(time.getHour());System.out.println(time.getMinute());System.out.println(time.getSecond());}

LocalDateTime

  • Java LocalDateTime is an immutable class, so it’s thread safe and suitable to use in multithreaded environment.

  • LocalDateTime provides date and time output in the format of YYYY-MM-DD-hh-mm-ss.zzz, extendable to nanosecond precision. So we can store “2017-11-10 21:30:45.123456789” in LocalDateTime object.

  • Just like the LocalDate class, LocalDateTime has no time zone data. We can use LocalDateTime instance for general purpose date and time information.

  • LocalDateTime class implements Comparable, ChronoLocalDateTime, Temporal, TemporalAdjuster, TermporalAccessor and Serializable interfaces.

  • LocalDateTime is a final class, so we can’t extend it.

  • LocalDateTime is a value based class, so we should use equals() method to check if two instances of LocalDateTime are equal or not.

  • LocalDateTime class is a synergistic ‘combination’ of LocalDate and LocalTime with the contatenated format as shown in the figure above.

private static void testLocalDateTime() {LocalDate localDate = LocalDate.now();LocalTime time = LocalTime.now();LocalDateTime localDateTime = LocalDateTime.of(localDate, time);System.out.println(localDateTime.toString());LocalDateTime now = LocalDateTime.now();System.out.println(now);}

Instant

private static void testInstant() throws InterruptedException {Instant start = Instant.now();Thread.sleep(1000L);Instant end = Instant.now();Duration duration = Duration.between(start, end);System.out.println(duration.toMillis());}

Period

private static void testPeriod() {Period period = Period.between(LocalDate.of(2020, 5, 10),LocalDate.of(2021, 3, 4));System.out.println(period.getYears());System.out.println(period.getMonths());System.out.println(period.getDays());}

Duration

private static void testDuration() {LocalTime time = LocalTime.now();System.out.println(time);LocalTime beforeTime = time.minusHours(2);System.out.println(beforeTime);Duration duration = Duration.between(beforeTime,time );System.out.println(duration.toHours());}

format

private static void testDateFormat() {LocalDate localDate = LocalDate.now();String format1 = localDate.format(DateTimeFormatter.BASIC_ISO_DATE);System.out.println(format1);DateTimeFormatter mySelfFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");String format = localDate.format(mySelfFormatter);System.out.println(format);}

parse

private static void testDateParse() {String date1 = "20210305";LocalDate localDate = LocalDate.parse(date1, DateTimeFormatter.BASIC_ISO_DATE);System.out.println(localDate);DateTimeFormatter mySelfFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");String date2 = "2021-03-05";LocalDate localDate2 = LocalDate.parse(date2, mySelfFormatter);System.out.println(localDate2);}

總結

以上是生活随笔為你收集整理的Java 8 - 时间API的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。