Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

(Simple)DateFormat that allow 24:00:00 and 00:00:00 as inputs

I've been looking for this for a while, with no success so far. Do you know if there's a "DateFormat" ish class, that will allow me to use "00:00:00" and "24:00:00" as input parameters (they're both midnight) but when called "getHour()" I'll get 0 or 24 as a result?

Using "kk" will only allow me to have <1:24> range, meanwhile I'm looking for <0:24> range formatting

like image 824
amerykanin Avatar asked Jun 29 '15 00:06

amerykanin


People also ask

What can I use instead of SimpleDateFormat?

You can use the DateTimeFormatter class in JDK 8 to change the date format of String in Java 8. The steps are exactly the same but instead of using SimpleDateFormat and Date class, we'll use the DateTimeFormatter and LocalDateTime class.

How do you format a date in Java eg in the Ddmmyyyy format?

Creating A Simple Date Format A SimpleDateFormat is a concrete class for formatting and parsing dates in a locale-sensitive manner. String pattern = "yyyy-MM-dd" ; SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern); The specified parameter “pattern” is the pattern used for formatting and parsing dates.

What is simple format?

Simple Data Format (SDF) is a platform-independent, precision-preserving binary data I/O format capable of handling large, multi-dimensional arrays. It was written in 2007 by George H. Fisher, a researcher at the Space Sciences Laboratory at UC Berkeley, and released under the GNU General Public License.


1 Answers

The value 24:00 is not represented in a LocalTime because it is strictly part of the next day. Consideration was given to models where 24:00 could be represented as part of LocalTime, but the conclusion was that it would be very confusing in a lot of use cases, and create more bugs than it solves.


There is support for 24:00 in java.time however. It is perfectly possible to parse it using the standard formatting techniques, however it is necessary to use SMART or LENIENT mode, see ResolverStyle. The default mode is SMART, however the formtter constants on DateTimeFormatter like DateTimeFormatter.ISO_LOCAL_DATE_TIME are in STRICT mode. Thus, ofPattern() defaults to SMART mode:

static final DateTimeFormatter FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm");

LocalDateTime ldt = LocalDateTime.parse("2012-12-03T24:00", FORMATTER);
System.out.println(ldt);  // 2012-12-04T00:00

Note that this also works for OffsetDateTime and ZonedDateTime. The standard parser for Instant supports 24:00 without a special formatter:

Instant instant = Instant.parse("2015-01-01T24:00:00Z");
System.out.println(instant);  // 2015-01-02T00:00:00Z

Any formatter can be converted to SMART or LENIENT mode using withResolverStyle() as follows:

DateTimeFormatter f = ...  // obtain a formatter somehow
DateTimeFormatter smartMode = f.withResolverStyle(ResolverStyle.SMART);

// for example
f = DateTimeFormatter.ISO_LOCAL_DATE_TIME.withResolverStyle(ResolverStyle.SMART);

The second element of support is parseExcessDays(). This allows the excess day to be obtained when only the time is being parsed:

static final DateTimeFormatter TIME_FORMATTER = DateTimeFormatter.ofPattern("HH:mm");

TemporalAccessor parsed = TIME_FORMATTER.parse("24:00");
LocalTime lt = LocalTime.from(parsed);
Period excessDays = parsed.query(DateTimeFormatter.parsedExcessDays());
System.out.println(lt + " + " + excessDays);  // 00:00 + P1D

Finally, a note for advanced users. It should in theory be possible to write your own implementation of Temporal that is a copy of LocalTime but with support for 24:00 as a valid value. Such a class, say LocalTimeWithEndOfDay, could then operate with the formatter/parser without issue (and might make a good addition to ThreeTen-Extra.

like image 113
JodaStephen Avatar answered Oct 29 '22 14:10

JodaStephen