@@ -145,8 +145,10 @@ public class StdDateFormat
145
145
146
146
/**
147
147
* Whether the TZ offset must be formatted with a colon between hours and minutes ({@code HH:mm} format)
148
+ *
149
+ * @since 2.9.1
148
150
*/
149
- private boolean _formatTzOffsetWithColumn = false ;
151
+ private boolean _tzSerializedWithColon = false ;
150
152
151
153
/*
152
154
/**********************************************************
@@ -164,11 +166,19 @@ public StdDateFormat(TimeZone tz, Locale loc) {
164
166
_locale = loc ;
165
167
}
166
168
167
- protected StdDateFormat (TimeZone tz , Locale loc , Boolean lenient , boolean formatTzOffsetWithColumn ) {
169
+ protected StdDateFormat (TimeZone tz , Locale loc , Boolean lenient ) {
170
+ this (tz , loc , lenient , false );
171
+ }
172
+
173
+ /**
174
+ * @since 2.9.1
175
+ */
176
+ protected StdDateFormat (TimeZone tz , Locale loc , Boolean lenient ,
177
+ boolean formatTzOffsetWithColon ) {
168
178
_timezone = tz ;
169
179
_locale = loc ;
170
180
_lenient = lenient ;
171
- _formatTzOffsetWithColumn = formatTzOffsetWithColumn ;
181
+ _tzSerializedWithColon = formatTzOffsetWithColon ;
172
182
}
173
183
174
184
public static TimeZone getDefaultTimeZone () {
@@ -186,31 +196,61 @@ public StdDateFormat withTimeZone(TimeZone tz) {
186
196
if ((tz == _timezone ) || tz .equals (_timezone )) {
187
197
return this ;
188
198
}
189
- return new StdDateFormat (tz , _locale , _lenient , _formatTzOffsetWithColumn );
199
+ return new StdDateFormat (tz , _locale , _lenient , _tzSerializedWithColon );
190
200
}
191
201
202
+ /**
203
+ * "Mutant factory" method that will return an instance that uses specified
204
+ * {@code Locale}:
205
+ * either {@code this} instance (if setting would not change), or newly
206
+ * constructed instance with different {@code Locale} to use.
207
+ */
192
208
public StdDateFormat withLocale (Locale loc ) {
193
209
if (loc .equals (_locale )) {
194
210
return this ;
195
211
}
196
- return new StdDateFormat (_timezone , loc , _lenient , _formatTzOffsetWithColumn );
212
+ return new StdDateFormat (_timezone , loc , _lenient , _tzSerializedWithColon );
197
213
}
198
214
199
215
/**
216
+ * "Mutant factory" method that will return an instance that has specified leniency
217
+ * setting: either {@code this} instance (if setting would not change), or newly
218
+ * constructed instance.
219
+ *
200
220
* @since 2.9
201
221
*/
202
222
public StdDateFormat withLenient (Boolean b ) {
203
223
if (_equals (b , _lenient )) {
204
224
return this ;
205
225
}
206
- return new StdDateFormat (_timezone , _locale , b , _formatTzOffsetWithColumn );
226
+ return new StdDateFormat (_timezone , _locale , b , _tzSerializedWithColon );
207
227
}
208
228
229
+ /**
230
+ * "Mutant factory" method that will return an instance that has specified
231
+ * handling of colon when serializing timezone (timezone either written
232
+ * like {@code +0500} or {@code +05:00}):
233
+ * either {@code this} instance (if setting would not change), or newly
234
+ * constructed instance with desired setting for colon inclusion.
235
+ *<p>
236
+ * NOTE: does NOT affect deserialization as colon is optional accepted
237
+ * but not required -- put another way, either serialization is accepted
238
+ * by this class.
239
+ *
240
+ * @since 2.9.1
241
+ */
242
+ public StdDateFormat withColonInTimeZone (boolean b ) {
243
+ if (_tzSerializedWithColon == b ) {
244
+ return this ;
245
+ }
246
+ return new StdDateFormat (_timezone , _locale , _lenient , b );
247
+ }
248
+
209
249
@ Override
210
250
public StdDateFormat clone () {
211
251
// Although there is that much state to share, we do need to
212
252
// orchestrate a bit, mostly since timezones may be changed
213
- return new StdDateFormat (_timezone , _locale , _lenient , _formatTzOffsetWithColumn );
253
+ return new StdDateFormat (_timezone , _locale , _lenient , _tzSerializedWithColon );
214
254
}
215
255
216
256
/**
@@ -287,26 +327,23 @@ public boolean isLenient() {
287
327
}
288
328
289
329
/**
290
- * If {@code true}), format TZ offset with a colon ({@code HH:mm}).
291
- *
292
- * @param formatTzOffsetWithColumn whether to include a colon in the formatted TZ
293
- */
294
- public void setFormatTzOffsetWithColumn (boolean formatTzOffsetWithColumn ) {
295
- this ._formatTzOffsetWithColumn = formatTzOffsetWithColumn ;
296
- }
297
-
298
- /**
299
- * Return {@code true} if a colon is to be inserted between the hours and minutes
300
- * of the TZ offset.
301
- *
330
+ * Accessor for checking whether this instance would include colon
331
+ * within timezone serialization or not: if {code true}, timezone offset
332
+ * is serialized like {@code -06:00}; if {code false} as {@code -0600}.
333
+ *<p>
334
+ * NOTE: only relevant for serialization (formatting), as deserialization
335
+ * (parsing) always accepts optional colon but does not require it, regardless
336
+ * of this setting.
337
+ *
302
338
* @return {@code true} if a colon is to be inserted between the hours and minutes
303
- * of the TZ offset
339
+ * of the TZ offset when serializing as String; otherwise {@code false}
340
+ *
341
+ * @since 2.9.1
304
342
*/
305
- public boolean isFormatTzOffsetWithColumn () {
306
- return _formatTzOffsetWithColumn ;
307
- }
308
-
309
-
343
+ public boolean isColonIncludedInTimeZone () {
344
+ return _tzSerializedWithColon ;
345
+ }
346
+
310
347
/*
311
348
/**********************************************************
312
349
/* Public API, parsing
@@ -425,15 +462,15 @@ protected void _format(TimeZone tz, Locale loc, Date date,
425
462
int minutes = Math .abs ((offset / (60 * 1000 )) % 60 );
426
463
buffer .append (offset < 0 ? '-' : '+' );
427
464
pad2 (buffer , hours );
428
- if ( _formatTzOffsetWithColumn ) {
465
+ if ( _tzSerializedWithColon ) {
429
466
buffer .append (':' );
430
467
}
431
468
pad2 (buffer , minutes );
432
469
} else {
433
470
// 24-Jun-2017, tatu: While `Z` would be conveniently short, older specs
434
471
// mandate use of full `+0000`
435
472
// formatted.append('Z');
436
- if ( _formatTzOffsetWithColumn ) {
473
+ if ( _tzSerializedWithColon ) {
437
474
buffer .append ("+00:00" );
438
475
}
439
476
else {
0 commit comments