• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

TAKETODAY / today-infrastructure / 16239319614

12 Jul 2025 03:12PM UTC coverage: 81.768% (-0.01%) from 81.778%
16239319614

Pull #285

github

web-flow
Merge 01aa6cdf2 into 05b234ebb
Pull Request #285: Java 24

59436 of 77647 branches covered (76.55%)

Branch coverage included in aggregate %.

140747 of 167171 relevant lines covered (84.19%)

3.6 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

91.25
today-context/src/main/java/infra/format/datetime/standard/DateTimeFormatterFactory.java
1
/*
2
 * Copyright 2017 - 2024 the original author or authors.
3
 *
4
 * This program is free software: you can redistribute it and/or modify
5
 * it under the terms of the GNU General Public License as published by
6
 * the Free Software Foundation, either version 3 of the License, or
7
 * (at your option) any later version.
8
 *
9
 * This program is distributed in the hope that it will be useful,
10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
 * GNU General Public License for more details.
13
 *
14
 * You should have received a copy of the GNU General Public License
15
 * along with this program. If not, see [https://www.gnu.org/licenses/]
16
 */
17

18
package infra.format.datetime.standard;
19

20
import java.time.format.DateTimeFormatter;
21
import java.time.format.FormatStyle;
22
import java.util.TimeZone;
23

24
import infra.format.annotation.DateTimeFormat.ISO;
25
import infra.lang.Assert;
26
import infra.lang.Nullable;
27
import infra.util.StringUtils;
28

29
/**
30
 * Factory that creates a JSR-310 {@link DateTimeFormatter}.
31
 *
32
 * <p>Formatters will be created using the defined {@link #setPattern pattern},
33
 * {@link #setIso ISO}, and <code>xxxStyle</code> methods (considered in that order).
34
 *
35
 * @author Juergen Hoeller
36
 * @author Phillip Webb
37
 * @author Sam Brannen
38
 * @author <a href="https://github.com/TAKETODAY">Harry Yang</a>
39
 * @see #createDateTimeFormatter()
40
 * @see #createDateTimeFormatter(DateTimeFormatter)
41
 * @see #setPattern
42
 * @see #setIso
43
 * @see #setDateStyle
44
 * @see #setTimeStyle
45
 * @see #setDateTimeStyle
46
 * @see DateTimeFormatterFactoryBean
47
 * @since 4.0
48
 */
49
public class DateTimeFormatterFactory {
50

51
  @Nullable
52
  private String pattern;
53

54
  @Nullable
55
  private ISO iso;
56

57
  @Nullable
58
  private FormatStyle dateStyle;
59

60
  @Nullable
61
  private FormatStyle timeStyle;
62

63
  @Nullable
64
  private TimeZone timeZone;
65

66
  /**
67
   * Create a new {@code DateTimeFormatterFactory} instance.
68
   */
69
  public DateTimeFormatterFactory() {
2✔
70
  }
1✔
71

72
  /**
73
   * Create a new {@code DateTimeFormatterFactory} instance.
74
   *
75
   * @param pattern the pattern to use to format date values
76
   */
77
  public DateTimeFormatterFactory(String pattern) {
2✔
78
    this.pattern = pattern;
3✔
79
  }
1✔
80

81
  /**
82
   * Set the pattern to use to format date values.
83
   *
84
   * @param pattern the format pattern
85
   */
86
  public void setPattern(String pattern) {
87
    this.pattern = pattern;
3✔
88
  }
1✔
89

90
  /**
91
   * Set the ISO format used to format date values.
92
   *
93
   * @param iso the ISO format
94
   */
95
  public void setIso(ISO iso) {
96
    this.iso = iso;
3✔
97
  }
1✔
98

99
  /**
100
   * Set the style to use for date types.
101
   */
102
  public void setDateStyle(FormatStyle dateStyle) {
103
    this.dateStyle = dateStyle;
3✔
104
  }
1✔
105

106
  /**
107
   * Set the style to use for time types.
108
   */
109
  public void setTimeStyle(FormatStyle timeStyle) {
110
    this.timeStyle = timeStyle;
3✔
111
  }
1✔
112

113
  /**
114
   * Set the style to use for date and time types.
115
   */
116
  public void setDateTimeStyle(FormatStyle dateTimeStyle) {
117
    this.dateStyle = dateTimeStyle;
3✔
118
    this.timeStyle = dateTimeStyle;
3✔
119
  }
1✔
120

121
  /**
122
   * Set the two characters to use to format date values.
123
   * <p>The first character is used for the date style; the second is for
124
   * the time style. Supported characters are:
125
   * <ul>
126
   * <li>'S' = Small</li>
127
   * <li>'M' = Medium</li>
128
   * <li>'L' = Long</li>
129
   * <li>'F' = Full</li>
130
   * <li>'-' = Omitted</li>
131
   * </ul>
132
   * <p>Note that JSR-310 natively favors {@link java.time.format.FormatStyle}
133
   * as used for {@link #setDateStyle}, {@link #setTimeStyle}, and
134
   * {@link #setDateTimeStyle}.
135
   *
136
   * @param style two characters from the set {"S", "M", "L", "F", "-"}
137
   */
138
  public void setStylePattern(String style) {
139
    Assert.isTrue(style.length() == 2, "Style pattern must consist of two characters");
8!
140
    this.dateStyle = convertStyleCharacter(style.charAt(0));
7✔
141
    this.timeStyle = convertStyleCharacter(style.charAt(1));
7✔
142
  }
1✔
143

144
  @Nullable
145
  private FormatStyle convertStyleCharacter(char c) {
146
    return switch (c) {
3!
147
      case 'S' -> FormatStyle.SHORT;
2✔
148
      case 'M' -> FormatStyle.MEDIUM;
2✔
149
      case 'L' -> FormatStyle.LONG;
2✔
150
      case 'F' -> FormatStyle.FULL;
×
151
      case '-' -> null;
2✔
152
      default -> throw new IllegalArgumentException("Invalid style character '" + c + "'");
×
153
    };
154
  }
155

156
  /**
157
   * Set the {@code TimeZone} to normalize the date values into, if any.
158
   *
159
   * @param timeZone the time zone
160
   */
161
  public void setTimeZone(TimeZone timeZone) {
162
    this.timeZone = timeZone;
3✔
163
  }
1✔
164

165
  /**
166
   * Create a new {@code DateTimeFormatter} using this factory.
167
   * <p>If no specific pattern or style has been defined,
168
   * {@link FormatStyle#MEDIUM medium date time format} will be used.
169
   *
170
   * @return a new date time formatter
171
   * @see #createDateTimeFormatter(DateTimeFormatter)
172
   */
173
  public DateTimeFormatter createDateTimeFormatter() {
174
    return createDateTimeFormatter(DateTimeFormatter.ofLocalizedDateTime(FormatStyle.MEDIUM));
5✔
175
  }
176

177
  /**
178
   * Create a new {@code DateTimeFormatter} using this factory.
179
   * <p>If no specific pattern or style has been defined,
180
   * the supplied {@code fallbackFormatter} will be used.
181
   *
182
   * @param fallbackFormatter the fall-back formatter to use
183
   * when no specific factory properties have been set
184
   * @return a new date time formatter
185
   */
186
  public DateTimeFormatter createDateTimeFormatter(DateTimeFormatter fallbackFormatter) {
187
    DateTimeFormatter dateTimeFormatter = null;
2✔
188
    if (StringUtils.isNotEmpty(this.pattern)) {
4✔
189
      dateTimeFormatter = DateTimeFormatterUtils.createStrictDateTimeFormatter(this.pattern);
5✔
190
    }
191
    else if (this.iso != null && this.iso != ISO.NONE) {
7✔
192
      dateTimeFormatter = switch (this.iso) {
8!
193
        case DATE -> DateTimeFormatter.ISO_DATE;
2✔
194
        case TIME -> DateTimeFormatter.ISO_TIME;
2✔
195
        case DATE_TIME -> DateTimeFormatter.ISO_DATE_TIME;
2✔
196
        default -> throw new IllegalStateException("Unsupported ISO format: " + this.iso);
×
197
      };
198
    }
199
    else if (this.dateStyle != null && this.timeStyle != null) {
6✔
200
      dateTimeFormatter = DateTimeFormatter.ofLocalizedDateTime(this.dateStyle, this.timeStyle);
7✔
201
    }
202
    else if (this.dateStyle != null) {
3✔
203
      dateTimeFormatter = DateTimeFormatter.ofLocalizedDate(this.dateStyle);
5✔
204
    }
205
    else if (this.timeStyle != null) {
3✔
206
      dateTimeFormatter = DateTimeFormatter.ofLocalizedTime(this.timeStyle);
4✔
207
    }
208

209
    if (dateTimeFormatter != null && this.timeZone != null) {
5✔
210
      dateTimeFormatter = dateTimeFormatter.withZone(this.timeZone.toZoneId());
6✔
211
    }
212
    return (dateTimeFormatter != null ? dateTimeFormatter : fallbackFormatter);
6✔
213
  }
214

215
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE TRIAL · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc