当前位置:   article > 正文

关于 spring boot 的 Duration (java.time.Duration) 在yml properties 中的配置方法_text cannot be parsed to a duration

text cannot be parsed to a duration

在新版本的spring boot中的redis的时间相关的配置使用了 java.time.Duration类

在配置时间时发现与老版本不同,就研究了下,发现使用了新的方式配置时间,这里记录下

从源码中可以看出 时间配置应该诸如: 1s 1.5s 0s 0.001S  1h 2d 1m 1M -PT0.001S P1DT2H15M(1天+2小时+15分钟) 形式

内部源码详解:

1. 我们可通过源码找到springboot的转换器StringToDurationConverter,其中转换的核心代码使用了DurationStyle定义了时间格式:

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

2. 我们在DurationStyle中可以看到有两种格式(简单格式和ISO-8601格式)的支持:

  1. /**
  2. * Simple formatting, for example '1s'.(简单格式)
  3. */
  4. SIMPLE("^([+-]?\\d+)([a-zA-Z]{0,2})$") {
  5. ......
  6. },
  7. /**
  8. * ISO-8601 formatting. (ISO-8601格式)
  9. */
  10. ISO8601("^[+-]?[pP].*$") {
  11. ......
  12. };

 简单格式:

DurationStyle中简单格式支持的单位定义在下方的Unit枚举中:

ns: 纳秒, us: 微秒, ms: 毫秒, s: 秒, m: 分钟, h: 小时, d: 天; (都不区分大小写)

  1. /**
  2. * Units that we support.
  3. */
  4. enum Unit {
  5. /**
  6. * Nanoseconds.
  7. */
  8. NANOS(ChronoUnit.NANOS, "ns", Duration::toNanos),
  9. /**
  10. * Microseconds.
  11. */
  12. MICROS(ChronoUnit.MICROS, "us", (duration) -> duration.toNanos() / 1000L),
  13. /**
  14. * Milliseconds.
  15. */
  16. MILLIS(ChronoUnit.MILLIS, "ms", Duration::toMillis),
  17. /**
  18. * Seconds.
  19. */
  20. SECONDS(ChronoUnit.SECONDS, "s", Duration::getSeconds),
  21. /**
  22. * Minutes.
  23. */
  24. MINUTES(ChronoUnit.MINUTES, "m", Duration::toMinutes),
  25. /**
  26. * Hours.
  27. */
  28. HOURS(ChronoUnit.HOURS, "h", Duration::toHours),
  29. /**
  30. * Days.
  31. */
  32. DAYS(ChronoUnit.DAYS, "d", Duration::toDays);

ISO-8601格式:

格式说明

采用ISO-8601时间格式。格式为:PnYnMnDTnHnMnS   (n为个数)

例如:P1Y2M3DT4H5M6.7S = 1年2个月3天4小时5分钟6.7秒

P:开始标记

1Y:1年 (Duration中没有)

2M:2个月 (Duration中没有)

3D:3天

T:日期和时间的分割标记

4H:4个小时

5M:5分钟

6.7S:6.7秒

注意: 这里的Duration只有D,H,M,S没有Y,M

详解

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

2.可以用“-”表示负数

示例:

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

源码介绍:

ISO-8601格式在DurationStyle中直接是使用Duration.parse方法进行处理,Duration.parse方法的定义如下:

  1. /**
  2. * Obtains a {@code Duration} from a text string such as {@code PnDTnHnMn.nS}.
  3. * <p>
  4. * This will parse a textual representation of a duration, including the
  5. * string produced by {@code toString()}. The formats accepted are based
  6. * on the ISO-8601 duration format {@code PnDTnHnMn.nS} with days
  7. * considered to be exactly 24 hours.
  8. * <p>
  9. * The string starts with an optional sign, denoted by the ASCII negative
  10. * or positive symbol. If negative, the whole period is negated.
  11. * The ASCII letter "P" is next in upper or lower case.
  12. * There are then four sections, each consisting of a number and a suffix.
  13. * The sections have suffixes in ASCII of "D", "H", "M" and "S" for
  14. * days, hours, minutes and seconds, accepted in upper or lower case.
  15. * The suffixes must occur in order. The ASCII letter "T" must occur before
  16. * the first occurrence, if any, of an hour, minute or second section.
  17. * At least one of the four sections must be present, and if "T" is present
  18. * there must be at least one section after the "T".
  19. * The number part of each section must consist of one or more ASCII digits.
  20. * The number may be prefixed by the ASCII negative or positive symbol.
  21. * The number of days, hours and minutes must parse to an {@code long}.
  22. * The number of seconds must parse to an {@code long} with optional fraction.
  23. * The decimal point may be either a dot or a comma.
  24. * The fractional part may have from zero to 9 digits.
  25. * <p>
  26. * The leading plus/minus sign, and negative values for other units are
  27. * not part of the ISO-8601 standard.
  28. * <p>
  29. * Examples:
  30. * <pre>
  31. * "PT20.345S" -- parses as "20.345 seconds"
  32. * "PT15M" -- parses as "15 minutes" (where a minute is 60 seconds)
  33. * "PT10H" -- parses as "10 hours" (where an hour is 3600 seconds)
  34. * "P2D" -- parses as "2 days" (where a day is 24 hours or 86400 seconds)
  35. * "P2DT3H4M" -- parses as "2 days, 3 hours and 4 minutes"
  36. * "P-6H3M" -- parses as "-6 hours and +3 minutes"
  37. * "-P6H3M" -- parses as "-6 hours and -3 minutes"
  38. * "-P-6H+3M" -- parses as "+6 hours and -3 minutes"
  39. * </pre>
  40. *
  41. * @param text the text to parse, not null
  42. * @return the parsed duration, not null
  43. * @throws DateTimeParseException if the text cannot be parsed to a duration
  44. */
  45. public static Duration parse(CharSequence text) {
  46. Objects.requireNonNull(text, "text");
  47. Matcher matcher = PATTERN.matcher(text);
  48. if (matcher.matches()) {
  49. // check for letter T but no time sections
  50. if ("T".equals(matcher.group(3)) == false) {
  51. boolean negate = "-".equals(matcher.group(1));
  52. String dayMatch = matcher.group(2);
  53. String hourMatch = matcher.group(4);
  54. String minuteMatch = matcher.group(5);
  55. String secondMatch = matcher.group(6);
  56. String fractionMatch = matcher.group(7);
  57. if (dayMatch != null || hourMatch != null || minuteMatch != null || secondMatch != null) {
  58. long daysAsSecs = parseNumber(text, dayMatch, SECONDS_PER_DAY, "days");
  59. long hoursAsSecs = parseNumber(text, hourMatch, SECONDS_PER_HOUR, "hours");
  60. long minsAsSecs = parseNumber(text, minuteMatch, SECONDS_PER_MINUTE, "minutes");
  61. long seconds = parseNumber(text, secondMatch, 1, "seconds");
  62. int nanos = parseFraction(text, fractionMatch, seconds < 0 ? -1 : 1);
  63. try {
  64. return create(negate, daysAsSecs, hoursAsSecs, minsAsSecs, seconds, nanos);
  65. } catch (ArithmeticException ex) {
  66. throw (DateTimeParseException) new DateTimeParseException("Text cannot be parsed to a Duration: overflow", text, 0).initCause(ex);
  67. }
  68. }
  69. }
  70. }
  71. throw new DateTimeParseException("Text cannot be parsed to a Duration", text, 0);
  72. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/从前慢现在也慢/article/detail/157162?site
推荐阅读
相关标签
  

闽ICP备14008679号