GithubHelp home page GithubHelp logo

Comments (11)

jodastephen avatar jodastephen commented on May 24, 2024

Joda-Time has tended to have more lenient parsing than formatting, this is one example. Its not going to change now (backward compatibility and upcoming JSR-310) but it could be better documented.

from joda-time.

michael-o avatar michael-o commented on May 24, 2024

OK but documentation must be improved. Can you add a boolean flag for strict parsing?

from joda-time.

michael-o avatar michael-o commented on May 24, 2024

From JSR 310 JavaDoc:

"Number: If the count of letters is one, then the value is printed using the minimum number of digits and without padding as per DateTimeFormatterBuilder.appendValue(java.time.temporal.TemporalField). Otherwise, the count of digits is used as the width of the output field as per DateTimeFormatterBuilder.appendValue(java.time.temporal.TemporalField, int)."

I fail to find the notion that this is non-strict parsing. SimpleDateFormat explicitly mentions this.

from joda-time.

jodastephen avatar jodastephen commented on May 24, 2024

JSR-310 has explicit strict/lenient/smart modes.

Joda-Time has an explicit appendFixedDecimal method on the builder class that can be used to obtain strict parsing.

from joda-time.

michael-o avatar michael-o commented on May 24, 2024

Stephen, the method you provided me does not seem to be very intuitive. I have already provided in the pattern 'yyyy-MM' how may digits I want, why do I need to repeat it? Cannot this be done automatically, if I configure a boolean strict on a formatter?

from joda-time.

jodastephen avatar jodastephen commented on May 24, 2024

You use DateTimeFormatterBuilder instead of using a pattern. The patterns are there for the common cases, if you have more specific cases not covered then the API provides the builder.

from joda-time.

michael-o avatar michael-o commented on May 24, 2024

I have evaluated the builder and this works:

DateTimeFormatterBuilder builder = new DateTimeFormatterBuilder();
builder.appendFixedDecimal(DateTimeFieldType.year(), 4);
builder.appendLiteral('-');
builder.appendFixedDecimal(DateTimeFieldType.monthOfYear(), 2);
builder.appendLiteral('-');
builder.appendFixedDecimal(DateTimeFieldType.dayOfMonth(), 2);
DateTimeFormatter parser = builder.toFormatter();

LocalDate date = parser.parseLocalDate("2013-9-13");
System.out.println(date);

This fails as desired. This has two implications:

  1. It is impossible to parse strict with a given string pattern
  2. The ISODateTimeFormat is broken because this 2013-9-14 is not a valid ISO date string but
DateTimeFormatter isoParser = ISODateTimeFormat.date();

LocalDate date = isoParser.parseLocalDate("2013-9-14");
System.out.println(date);

Accepts this input. Shall I raise another issue?

from joda-time.

jodastephen avatar jodastephen commented on May 24, 2024

If you want to raise another issue, you can, however I can't change ISODateTimeFormat due to backward compatibility. The best I could do would be more documentation. And yes, it is far from ideal as is.

Adding a dateStrict() method would be possible if it weren't for the fact that there are lots and lots of methods in that class.

A better issue would be the addition of a strict mode, however that would be a big change at this point in Joda-Time's life and not one I'm planning on making.

from joda-time.

michael-o avatar michael-o commented on May 24, 2024

This is a pity. I simply do not understand why this issue has not been discovered before.

A strict mode would be perfect. At least, what about a ISODateTimeStrictFormat with a improved along its way?

from joda-time.

jodastephen avatar jodastephen commented on May 24, 2024

If you want to create a pull request with a StrictISODateTimeFormat class I'd probably accept that. It wouldn't have to have all of the formats from the current class, just a selection of useful ones. It would need some tests to be accepted ;-)

from joda-time.

michael-o avatar michael-o commented on May 24, 2024

Stephen, that's fine. I would create a pull request in time. Are you able to improve the documentation of the ISODateTimeFormat class? It should crearly say that it does not comply with ISO 8601.

from joda-time.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.