欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

springboot之Duration(java.time.Duration)在yml properties中的配置方式

 更新時(shí)間:2023年12月18日 15:18:43   作者:hank009  
這篇文章主要介紹了springboot之Duration(java.time.Duration)在yml properties中的配置方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教

在新版本的spring boot中的redis的時(shí)間相關(guān)的配置使用了 java.time.Duration類

在配置時(shí)間時(shí)發(fā)現(xiàn)與老版本不同,就研究了下,發(fā)現(xiàn)使用了新的方式配置時(shí)間,這里記錄下

從源碼中可以看出 時(shí)間配置應(yīng)該諸如: 1s 1.5s 0s 0.001S  1h 2d 1m 1M -PT0.001S P1DT2H15M(1天+2小時(shí)+15分鐘) 形式

內(nèi)部源碼詳解

1. 我們可通過(guò)源碼找到springboot的轉(zhuǎn)換器StringToDurationConverter,其中轉(zhuǎn)換的核心代碼使用了DurationStyle定義了時(shí)間格式:

	private Duration convert(String source, DurationStyle style, ChronoUnit unit) {
		style = (style != null) ? style : DurationStyle.detect(source);
		return style.parse(source, unit);
	}

2. 我們?cè)?strong>DurationStyle中可以看到有兩種格式(簡(jiǎn)單格式和ISO-8601格式)的支持:

	/**
	 * Simple formatting, for example '1s'.(簡(jiǎn)單格式)
	 */
	SIMPLE("^([+-]?\\d+)([a-zA-Z]{0,2})$") {
 
		......
	},
 
	/**
	 * ISO-8601 formatting. (ISO-8601格式)
	 */
	ISO8601("^[+-]?[pP].*$") {
 
		......
	};

簡(jiǎn)單格式

DurationStyle中簡(jiǎn)單格式支持的單位定義在下方的Unit枚舉中:

  • ns: 納秒 
  • us: 微秒 
  • ms: 毫秒 
  • s: 秒
  • m: 分鐘 
  • h: 小時(shí) 
  • d: 天; (都不區(qū)分大小寫)
	/**
	 * Units that we support.
	 */
	enum Unit {
 
		/**
		 * Nanoseconds.
		 */
		NANOS(ChronoUnit.NANOS, "ns", Duration::toNanos),
 
		/**
		 * Microseconds.
		 */
		MICROS(ChronoUnit.MICROS, "us", (duration) -> duration.toNanos() / 1000L),
 
		/**
		 * Milliseconds.
		 */
		MILLIS(ChronoUnit.MILLIS, "ms", Duration::toMillis),
 
		/**
		 * Seconds.
		 */
		SECONDS(ChronoUnit.SECONDS, "s", Duration::getSeconds),
 
		/**
		 * Minutes.
		 */
		MINUTES(ChronoUnit.MINUTES, "m", Duration::toMinutes),
 
		/**
		 * Hours.
		 */
		HOURS(ChronoUnit.HOURS, "h", Duration::toHours),
 
		/**
		 * Days.
		 */
		DAYS(ChronoUnit.DAYS, "d", Duration::toDays);

ISO-8601格式

格式說(shuō)明

采用ISO-8601時(shí)間格式。格式為:PnYnMnDTnHnMnS   (n為個(gè)數(shù))

例如:P1Y2M3DT4H5M6.7S = 1年2個(gè)月3天4小時(shí)5分鐘6.7秒

P:開始標(biāo)記

  • 1Y:1年 (Duration中沒有)
  • 2M:2個(gè)月 (Duration中沒有)
  • 3D:3天

T:日期和時(shí)間的分割標(biāo)記

  • 4H:4個(gè)小時(shí)
  • 5M:5分鐘
  • 6.7S:6.7秒

注意: 這里的Duration只有D,H,M,S沒有Y,M

詳解

1."P", "D", "H", "M" 和 "S"可以是大寫或者小寫(建議大寫)

2.可以用“-”表示負(fù)數(shù)

示例:

          "PT20.345S" -- parses as "20.345 seconds"
          "PT15M"     -- parses as "15 minutes" (where a minute is 60 seconds)
          "PT10H"     -- parses as "10 hours" (where an hour is 3600 seconds)
          "P2D"       -- parses as "2 days" (where a day is 24 hours or 86400 seconds)
          "P2DT3H4M"  -- parses as "2 days, 3 hours and 4 minutes"
          "PT-6H3M"    -- parses as "-6 hours and +3 minutes"
          "-PT6H3M"    -- parses as "-6 hours and -3 minutes"
          "-PT-6H+3M"  -- parses as "+6 hours and -3 minutes"

源碼介紹

ISO-8601格式在DurationStyle中直接是使用Duration.parse方法進(jìn)行處理,Duration.parse方法的

定義如下:

/**
     * Obtains a {@code Duration} from a text string such as {@code PnDTnHnMn.nS}.
     * <p>
     * This will parse a textual representation of a duration, including the
     * string produced by {@code toString()}. The formats accepted are based
     * on the ISO-8601 duration format {@code PnDTnHnMn.nS} with days
     * considered to be exactly 24 hours.
     * <p>
     * The string starts with an optional sign, denoted by the ASCII negative
     * or positive symbol. If negative, the whole period is negated.
     * The ASCII letter "P" is next in upper or lower case.
     * There are then four sections, each consisting of a number and a suffix.
     * The sections have suffixes in ASCII of "D", "H", "M" and "S" for
     * days, hours, minutes and seconds, accepted in upper or lower case.
     * The suffixes must occur in order. The ASCII letter "T" must occur before
     * the first occurrence, if any, of an hour, minute or second section.
     * At least one of the four sections must be present, and if "T" is present
     * there must be at least one section after the "T".
     * The number part of each section must consist of one or more ASCII digits.
     * The number may be prefixed by the ASCII negative or positive symbol.
     * The number of days, hours and minutes must parse to an {@code long}.
     * The number of seconds must parse to an {@code long} with optional fraction.
     * The decimal point may be either a dot or a comma.
     * The fractional part may have from zero to 9 digits.
     * <p>
     * The leading plus/minus sign, and negative values for other units are
     * not part of the ISO-8601 standard.
     * <p>
     * Examples:
     * <pre>
     *    "PT20.345S" -- parses as "20.345 seconds"
     *    "PT15M"     -- parses as "15 minutes" (where a minute is 60 seconds)
     *    "PT10H"     -- parses as "10 hours" (where an hour is 3600 seconds)
     *    "P2D"       -- parses as "2 days" (where a day is 24 hours or 86400 seconds)
     *    "P2DT3H4M"  -- parses as "2 days, 3 hours and 4 minutes"
     *    "P-6H3M"    -- parses as "-6 hours and +3 minutes"
     *    "-P6H3M"    -- parses as "-6 hours and -3 minutes"
     *    "-P-6H+3M"  -- parses as "+6 hours and -3 minutes"
     * </pre>
     *
     * @param text  the text to parse, not null
     * @return the parsed duration, not null
     * @throws DateTimeParseException if the text cannot be parsed to a duration
     */
    public static Duration parse(CharSequence text) {
        Objects.requireNonNull(text, "text");
        Matcher matcher = PATTERN.matcher(text);
        if (matcher.matches()) {
            // check for letter T but no time sections
            if ("T".equals(matcher.group(3)) == false) {
                boolean negate = "-".equals(matcher.group(1));
                String dayMatch = matcher.group(2);
                String hourMatch = matcher.group(4);
                String minuteMatch = matcher.group(5);
                String secondMatch = matcher.group(6);
                String fractionMatch = matcher.group(7);
                if (dayMatch != null || hourMatch != null || minuteMatch != null || secondMatch != null) {
                    long daysAsSecs = parseNumber(text, dayMatch, SECONDS_PER_DAY, "days");
                    long hoursAsSecs = parseNumber(text, hourMatch, SECONDS_PER_HOUR, "hours");
                    long minsAsSecs = parseNumber(text, minuteMatch, SECONDS_PER_MINUTE, "minutes");
                    long seconds = parseNumber(text, secondMatch, 1, "seconds");
                    int nanos = parseFraction(text,  fractionMatch, seconds < 0 ? -1 : 1);
                    try {
                        return create(negate, daysAsSecs, hoursAsSecs, minsAsSecs, seconds, nanos);
                    } catch (ArithmeticException ex) {
                        throw (DateTimeParseException) new DateTimeParseException("Text cannot be parsed to a Duration: overflow", text, 0).initCause(ex);
                    }
                }
            }
        }
        throw new DateTimeParseException("Text cannot be parsed to a Duration", text, 0);
    }

總結(jié)

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • SpringBoot詳解如何進(jìn)行整合Druid數(shù)據(jù)源

    SpringBoot詳解如何進(jìn)行整合Druid數(shù)據(jù)源

    Druid是阿里開發(fā)的一款開源的數(shù)據(jù)源,被很多人認(rèn)為是Java語(yǔ)言中最好的數(shù)據(jù)庫(kù)連接池,本文主要介紹了SpringBoot整合Druid數(shù)據(jù)源的方法實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2022-06-06
  • Java多線程的同步優(yōu)化的6種方案

    Java多線程的同步優(yōu)化的6種方案

    大家使用多線程無(wú)非是為了提高性能,在Java中,有多線程并發(fā)時(shí),我們可以使用多線程同步的方式來(lái)解決內(nèi)存一致性的問(wèn)題。本文就詳細(xì)的介紹了Java多線程同步優(yōu)化,感興趣的可以了解一下
    2021-05-05
  • java8 Math新增方法介紹

    java8 Math新增方法介紹

    這篇文章主要介紹了java8 Math新增方法介紹,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2020-08-08
  • java后臺(tái)如何利用Pattern提取所需字符詳解

    java后臺(tái)如何利用Pattern提取所需字符詳解

    這篇文章主要給大家介紹了關(guān)于java后臺(tái)如何利用Pattern提取所需字符的相關(guān)資料,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2022-01-01
  • Mockito mock Kotlin Object類方法報(bào)錯(cuò)解決方法

    Mockito mock Kotlin Object類方法報(bào)錯(cuò)解決方法

    這篇文章主要介紹了Mockito mock Kotlin Object類方法報(bào)錯(cuò)解決方法,本篇文章通過(guò)簡(jiǎn)要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下
    2021-09-09
  • Windows10安裝IDEA 2020.1.2的方法步驟

    Windows10安裝IDEA 2020.1.2的方法步驟

    這篇文章主要介紹了Windows10安裝IDEA 2020.1.2的方法步驟,文中通過(guò)圖文介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-08-08
  • java反射實(shí)現(xiàn)javabean轉(zhuǎn)json實(shí)例代碼

    java反射實(shí)現(xiàn)javabean轉(zhuǎn)json實(shí)例代碼

    基于java反射機(jī)制實(shí)現(xiàn)javabean轉(zhuǎn)json字符串實(shí)例,大家參考使用吧
    2013-12-12
  • IntelliJ IDEA 無(wú)法正常使用SVN的問(wèn)題和完美解決辦法

    IntelliJ IDEA 無(wú)法正常使用SVN的問(wèn)題和完美解決辦法

    這篇文章主要介紹了IntelliJ IDEA 無(wú)法正常使用SVN的問(wèn)題和解決辦法,本文給大家分享完美解決方案,通過(guò)圖文并茂的形式給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2023-08-08
  • Spring?AOP概念及原理解析

    Spring?AOP概念及原理解析

    這篇文章主要介紹了Spring?AOP概念及原理?,通過(guò)使用?Spring?AOP?實(shí)現(xiàn)日志管理,我們可以將日志記錄的邏輯從業(yè)務(wù)邏輯中分離出來(lái),簡(jiǎn)化了代碼的維護(hù),需要的朋友可以參考下
    2024-07-07
  • JAVA文件讀寫例題實(shí)現(xiàn)過(guò)程解析

    JAVA文件讀寫例題實(shí)現(xiàn)過(guò)程解析

    這篇文章主要介紹了JAVA文件讀寫例題實(shí)現(xiàn)過(guò)程解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-06-06

最新評(píng)論