Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parsing Joda-Time Partials

Tags:

java

jodatime

I'd like to produce Partials from Strings, but can't find anything in the API that supports that. Obviously, I can write my own parser outside of the Joda-Time framework and create the Partials, but I can't imagine that the API doesn't already have the ability to do this.

Use of threeten (JSR-310) would be an acceptable solution, but it doesn't seem to support Partials. I don't know whether that is due to its alpha status, or whether the Partial concept is handled in a different manner, which I haven't discovered.

What is the best way to convert a String (2011, 02/11, etc) into a Partial?

like image 468
user371320 Avatar asked Feb 21 '11 16:02

user371320


People also ask

Is Joda Time deprecated?

So the short answer to your question is: YES (deprecated).

How do I parse LocalDateTime?

parse(CharSequence text) parse() method of a LocalDateTime class used to get an instance of LocalDateTime from a string such as '2018-10-23T17:19:33' passed as parameter. The string must have a valid date-time and is parsed using DateTimeFormatter. ISO_LOCAL_DATE_TIME.

What is the replacement of Joda time?

time (JSR-310) which is a core part of the JDK which replaces joda library project.

Is DateTimeFormatter thread safe?

Yes, it is: DateTimeFormat is thread-safe and immutable, and the formatters it returns are as well. Implementation Requirements: This class is immutable and thread-safe.


3 Answers

I've extended DateTimeParserBucket. My extended class intercepts calls to the saveField() methods, and stores the field type and value before delegating to super. I've also implemented a method that uses those stored field values to create a Partial.

I'm able to pass my bucket instance to DateTimeParser.parseInto(), and then ask it to create the Partial.

It works, but I can't say I'm impressed with Joda-Time - given that it doesn't support parsing Partials out of the box. The lack of DateTimeFormatter.parsePartial(String) is a glaring omission.

like image 116
user371320 Avatar answered Oct 08 '22 20:10

user371320


You have to start by defining the valid format for Partials which you will be accepting. There is no class which will just take text and infer the best possible match for a Partial. It's way too subjective based on locale, user preference, etc. So there's no way of getting around making a list of all of the valid formats for input. It will be very difficult to make these all mutually exclusive for each other, so there should be priorities. For example, you might want mm/dd and mm/yy to both be valid formats. If I give you the string 02/11, which one should have priority?

Once you've determined exactly the valid formats, you should use DateTimeFormat.forPattern to create a DateTimeFormatter for each one. Then you can use each formatter to try to parseInto a MutableDateTime. Then, go through each field in the MutableDateTime and transfer the value into a Partial.

Unfortunately, there is no better way to handle this in the Joda library.

like image 27
Erick Robertson Avatar answered Oct 08 '22 18:10

Erick Robertson


The ISODateTimeFormat class allows partial printing. As you say, there is no parsing method on DateTimeFormatter (although you can parse to a LocalDate and interpret that).

ThreeTen/JSR-310 has the DateTimeFields class which replaces Partial. Parsing of partials into a CalendricalMerger is supported, however that may not be convertable back into a DateTimeFields yet.

like image 37
JodaStephen Avatar answered Oct 08 '22 20:10

JodaStephen