coverting Joda dateOptionalTimeParser to java.time

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

coverting Joda dateOptionalTimeParser to java.time

costin
This might not be the appropriate list however I'm hoping library experts can chime in.

I'm trying to migrate from Joda to Java 8 java.time package but that doesn't seem possible.
I'm using Joda's ISODateTimeFormat.dateOptionalTimeParser but I can't seem to find anything
similar in java.time.
The culprit seems to be that Joda allows multiple DateFormats to be chained while this features
is not available in java.time.
As a concrete example, to quote Joda's dateOptionalTimeParser javadocs can parse the date in three
different formats: std-date-element | ord-date-element | week-date-element however java.time
allows only one.

While with Joda DateTimeFormatterBuilder, one can append multiple parsers, java.time allows only one.

Am I missing something?

Thanks,
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: coverting Joda dateOptionalTimeParser to java.time

jodastephen
java.time does not contain every Joda-Time feature. Things like this
can be simulated using multiple calls.

See also java.time DateTimeFormatterBuilder, which has optionalStart()
and optionalEnd(). These allow sections of the parse to be optional.
This could simulate the format roughly using:

yyyy-[DDD][-'W'ww-e][MM-dd]  (with a zone like Europe/London where 'e'
is day-of-week from Monday)

This will parse the three formats, however it will also parse the
combination "2015-001W02-301-01" It depends how strict you want to be.

Stephen



On 28 July 2015 at 18:52, costin <[hidden email]> wrote:

> This might not be the appropriate list however I'm hoping library experts can
> chime in.
>
> I'm trying to migrate from Joda to Java 8 java.time package but that doesn't
> seem possible.
> I'm using Joda's ISODateTimeFormat.dateOptionalTimeParser but I can't seem
> to find anything
> similar in java.time.
> The culprit seems to be that Joda allows multiple DateFormats to be chained
> while this features
> is not available in java.time.
> As a concrete example, to quote Joda's dateOptionalTimeParser javadocs can
> parse the date in three
> different formats: std-date-element | ord-date-element | week-date-element
> however java.time
> allows only one.
>
> While with Joda DateTimeFormatterBuilder, one can append multiple parsers,
> java.time allows only one.
>
> Am I missing something?
>
> Thanks,
>
>
>
> --
> View this message in context: http://joda-interest.219941.n2.nabble.com/coverting-Joda-dateOptionalTimeParser-to-java-time-tp7572605.html
> Sent from the Joda-Interest mailing list archive at Nabble.com.
>
> ------------------------------------------------------------------------------
> _______________________________________________
> Joda-interest mailing list
> [hidden email]
> https://lists.sourceforge.net/lists/listinfo/joda-interest

------------------------------------------------------------------------------
_______________________________________________
Joda-interest mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/joda-interest
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: coverting Joda dateOptionalTimeParser to java.time

costin
Thanks for the quick reply Stephen.

The main advantage that we found with Joda (thanks for working on it,
it's a brilliant piece of software), is that is quite lenient in
parsing the input while maintaining correctness; in particular it
allows (just like ISO 8601 indicates) for certain "tokens" inside the
date to be skipped.

Based on the Javadocs it looks like the complete grammar for dateOptionalTime would be:

     * date-opt-time     = date-element ['T' [time-element] [offset]]
     * date-element      = std-date-element | ord-date-element |
week-date-element
     * std-date-element  = yyyy ['-' MM ['-' dd]]
     * ord-date-element  = yyyy ['-' DDD]
     * week-date-element = xxxx '-W' ww ['-' e]
     * time-element      = HH [minute-element] | [fraction]
     * minute-element    = ':' mm [second-element] | [fraction]
     * second-element    = ':' ss [fraction]
     * fraction          = ('.' | ',') digit+

so based on using optional blocks, the syntax would be roughly (skipping fraction):

"yyyy-[MM[dd]][DDD][W'ww['-'e]]['T'[HH[':'mm[':'ss]]][Z]]"

However as you mentioned since there's no proper OR in java.time one
can pass in an invalid date; is there any solution for that?

Outside creating a formatter for each combination/permutation
(cartesian product) and trying to apply each one until it matches.

Cheers,
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: coverting Joda dateOptionalTimeParser to java.time

jodastephen
An "OR" concept would be a good addition in Java 9, but doesn't exist
now. At present, you'd have to try lots of combinations, or pre-parse
using a regex or similar,
Stephen

On 29 July 2015 at 08:28, costin <[hidden email]> wrote:

> Thanks for the quick reply Stephen.
>
> The main advantage that we found with Joda (thanks for working on it,
> it's a brilliant piece of software), is that is quite lenient in
> parsing the input while maintaining correctness; in particular it
> allows (just like ISO 8601 indicates) for certain "tokens" inside the
> date to be skipped.
>
> Based on the Javadocs it looks like the complete grammar for
> dateOptionalTime would be:
>
>      * date-opt-time     = date-element ['T' [time-element] [offset]]
>      * date-element      = std-date-element | ord-date-element |
> week-date-element
>      * std-date-element  = yyyy ['-' MM ['-' dd]]
>      * ord-date-element  = yyyy ['-' DDD]
>      * week-date-element = xxxx '-W' ww ['-' e]
>      * time-element      = HH [minute-element] | [fraction]
>      * minute-element    = ':' mm [second-element] | [fraction]
>      * second-element    = ':' ss [fraction]
>      * fraction          = ('.' | ',') digit+
>
> so based on using optional blocks, the syntax would be roughly (skipping
> fraction):
>
> "yyyy-[MM[dd]][DDD][W'ww['-'e]]['T'[HH[':'mm[':'ss]]][Z]]"
>
> However as you mentioned since there's no proper OR in java.time one
> can pass in an invalid date; is there any solution for that?
>
> Outside creating a formatter for each combination/permutation
> (cartesian product) and trying to apply each one until it matches.
>
> Cheers,
>
>
>
> --
> View this message in context: http://joda-interest.219941.n2.nabble.com/coverting-Joda-dateOptionalTimeParser-to-java-time-tp7572605p7572607.html
> Sent from the Joda-Interest mailing list archive at Nabble.com.
>
> ------------------------------------------------------------------------------
> _______________________________________________
> Joda-interest mailing list
> [hidden email]
> https://lists.sourceforge.net/lists/listinfo/joda-interest

------------------------------------------------------------------------------
_______________________________________________
Joda-interest mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/joda-interest
Loading...