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

TAKETODAY / today-infrastructure / 18154768944

01 Oct 2025 07:26AM UTC coverage: 81.882% (-0.005%) from 81.887%
18154768944

push

github

web-flow
Merge pull request #290 from TAKETODAY/dev/jspecify

jspecify

59788 of 78013 branches covered (76.64%)

Branch coverage included in aggregate %.

141239 of 167496 relevant lines covered (84.32%)

3.6 hits per line

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

36.0
today-context/src/main/java/infra/format/datetime/standard/DateTimeContext.java
1
/*
2
 * Copyright 2017 - 2025 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 org.jspecify.annotations.Nullable;
21

22
import java.time.ZoneId;
23
import java.time.chrono.Chronology;
24
import java.time.format.DateTimeFormatter;
25
import java.util.TimeZone;
26

27
import infra.core.i18n.LocaleContext;
28
import infra.core.i18n.LocaleContextHolder;
29
import infra.core.i18n.TimeZoneAwareLocaleContext;
30

31
/**
32
 * A context that holds user-specific <code>java.time</code> (JSR-310) settings
33
 * such as the user's Chronology (calendar system) and time zone.
34
 * <p>A {@code null} property value indicates the user has not specified a setting.
35
 *
36
 * @author Juergen Hoeller
37
 * @author <a href="https://github.com/TAKETODAY">Harry Yang</a>
38
 * @see DateTimeContextHolder
39
 * @since 4.0
40
 */
41
public class DateTimeContext {
3✔
42

43
  @Nullable
44
  private Chronology chronology;
45

46
  @Nullable
47
  private ZoneId timeZone;
48

49
  /**
50
   * Set the user's chronology (calendar system).
51
   */
52
  public void setChronology(@Nullable Chronology chronology) {
53
    this.chronology = chronology;
×
54
  }
×
55

56
  /**
57
   * Return the user's chronology (calendar system), if any.
58
   */
59
  @Nullable
60
  public Chronology getChronology() {
61
    return this.chronology;
×
62
  }
63

64
  /**
65
   * Set the user's time zone.
66
   * <p>Alternatively, set a {@link TimeZoneAwareLocaleContext} on
67
   * {@link LocaleContextHolder}. This context class will fall back to
68
   * checking the locale context if no setting has been provided here.
69
   *
70
   * @see LocaleContextHolder#getTimeZone()
71
   * @see LocaleContextHolder#setLocaleContext
72
   */
73
  public void setTimeZone(@Nullable ZoneId timeZone) {
74
    this.timeZone = timeZone;
3✔
75
  }
1✔
76

77
  /**
78
   * Return the user's time zone, if any.
79
   */
80
  @Nullable
81
  public ZoneId getTimeZone() {
82
    return this.timeZone;
×
83
  }
84

85
  /**
86
   * Get the DateTimeFormatter with this context's settings applied to the
87
   * base {@code formatter}.
88
   *
89
   * @param formatter the base formatter that establishes default
90
   * formatting rules, generally context-independent
91
   * @return the contextual DateTimeFormatter
92
   */
93
  public DateTimeFormatter getFormatter(DateTimeFormatter formatter) {
94
    if (this.chronology != null) {
3!
95
      formatter = formatter.withChronology(this.chronology);
×
96
    }
97
    if (this.timeZone != null) {
3!
98
      formatter = formatter.withZone(this.timeZone);
6✔
99
    }
100
    else {
101
      LocaleContext localeContext = LocaleContextHolder.getLocaleContext();
×
102
      if (localeContext instanceof TimeZoneAwareLocaleContext) {
×
103
        TimeZone timeZone = ((TimeZoneAwareLocaleContext) localeContext).getTimeZone();
×
104
        if (timeZone != null) {
×
105
          formatter = formatter.withZone(timeZone.toZoneId());
×
106
        }
107
      }
108
    }
109
    return formatter;
2✔
110
  }
111

112
}
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