Comments (11)
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.
OK but documentation must be improved. Can you add a boolean flag for strict parsing?
from joda-time.
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.
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.
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.
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.
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:
- It is impossible to parse strict with a given string pattern
- The
ISODateTimeFormat
is broken because this2013-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.
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.
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.
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.
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)
- Create a Tag on the latest release (v2.12.2) HOT 2
- Latest version is missing in the Release notes page HOT 1
- Support Java Modules HOT 13
- Asia/tehran time zone HOT 1
- Iana was update the timezonedb, when joda-time will updat? HOT 1
- New release for tzdb 2023b HOT 2
- DateTimeZone#forID throws exception for ZoneOffset.UTC.getId() HOT 1
- Crashes for the America/Ciudad_Juarez HOT 1
- Calculation of Duration between two DateTimes in March 2023 produces an incorrect result. HOT 1
- 13 seconds diff in <1912 Portuguese timestamps HOT 1
- Publish to MVN central HOT 3
- One year period calculation issue when start date = 29/02 HOT 1
- JodaTime uses java.lang.SecurityManager which is scheduled for deletion HOT 4
- Change JDK baseline (v3) HOT 1
- tzdb 2024a
- Expand Range of Years HOT 3
- How to migrate Joda time Period API to Java time (java 8)? HOT 2
- Request for Asia/Almaty Timezone Offset Change HOT 2
- Not able to serialize ZonedDateTime,PeriodDuration while converting bean to xml HOT 1
- Does 2.12.7 fixed CVE-2024-23080 ? HOT 5
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from joda-time.