This question already has answers here :
YYYY is week year, yyyy is year
So Change final String DATE_FORMAT = "YYYYMM";
ro final String DATE_FORMAT = "yyyyMM";
should give you the correct result. For more informations about the patterns see the javadoc of DateTimeFormatter .
The first week of 2019 starts at Dec 30 of 2018. See this link for more informations about the wee years
java - DateTimeFormatter giving wrong format for edge cases, DateTimeFormatter is not giving correct format for Dec 30 and 31 2018 as per following snippet. final String DATE_FORMAT = "YYYYMM"; DateTimeFormatter DateTimeFormatter giving wrong format for edge cases [duplicate] DateTimeFormatter is not giving correct format for Dec 30 and 31 2018 as per following snippet.
This is expected behaviour. YYYY
stands for "week-based-year", which is not the same as calendar year (see JavaDoc )
You most probably want to use yyyy
, which means "year-of-era"
DateTimeFormatter giving wrong format for edge cases [duplicate , DateTimeFormatter is not giving correct format for Dec 30 and 31 2018 as per following snippet. final String DATE_FORMAT = "YYYYMM"; DateTimeFormatter When you run DateAdd or Day function, the returned value of the function is US-locale format instead of the format that you defined. For example, the locale of operation system on the computer is UK locale (DMY) and the locale of SQL Server 2012 or SSAS 2014 is US locale (MDY).
y
is for "year-of-era" while Y
is for week-based-year
Replace:
final String DATE_FORMAT = "YYYYMM";
to:
final String DATE_FORMAT = "yyyyMM";
RangeError: invalid date, Examples. Invalid cases. Unrecognizable strings or dates containing illegal element values in ISO formatted strings usually return NaN . Some of the constructors for DateTimeFormatter take a formatTemplate string parameter that specifies the requested components to be formatted. This parameter can be either a format template or a format pattern. Format templates are convenient to use and provide formatting which is typical for the language, clock and calendar that you specify.
Intl.DateTimeFormat, The Intl.DateTimeFormat object is a constructor for objects that enable language-sensitive date and time formatting. console.log(new Intl.DateTimeFormat('en-US').format(date));. 5 Include a fallback language, in this case Indonesian. 11 Chrome, Edge, Firefox, Internet Explorer, Opera, Safari, Android This is an insidious problem, because in some cases you may get the right values, but not all of the time. I suspect it usually comes up again due to copy and paste, but often from specifications rather than other code – in a specification, it’s pretty clear what "YYYY-MM-DD HH:MM:SS" means as a date/time format, but that doesn’t mean it
ISO-8601, YYYY, yyyy, and why your year may be wrong, Some strange behavior in edge cases doesn't invalidate the benefits of Intl.DateTimeFormat is to intentionally not give you the specific format Excel Date Question - Incorrect Date Displaying? Hello, I am trying to format dates in one of my columns to where the user enters the date as a two-digit month, two-digit day and two-digit year in the cell (without dashes or slashes) where Excel will automatically format to include dashes and/or slashes in between.
wrong year with DateTimeFormatter · Issue #19 · JakeWharton , For example, January 1, 1998 is a Thursday. If getFirstDayOfWeek() is MONDAY and getMinimalDaysInFirstWeek() is 4 (ISO 8601 standard Setting the date-time to a fixed single offset means that any future calculations, such as addition or subtraction, have no complex edge cases due to time-zone rules. This might also be useful when sending a zoned date-time across a network, as most protocols, such as ISO-8601, only handle offsets, and not region-based zone IDs.
Comments did you recompile after you changed the code? YYYYis week year, yyyy is year Please read SimpleTimeFormatter javadoc carefully. Note that this is for Java 10. you need to understand mechanism of YYYY & yyyy. yyyy specifies the calendar year whereas YYYY specifies the year (of "Week of Year"), used in the ISO year-week calendar @Kandy that's why I linked to the hugely popular JodaTime library which "is the de facto standard date and time library for Java prior to Java SE 8", and which handles Y
as year of era (>=0)
- don't assume anything about which parser/formatter implements which (parts of the) standard unless it explicitly documents compliance. @OleV.V. The documentation of DateFormatter was not wo good until Java Version 7 so i liked to use the SimpleDateFormat documentation. I have seen it was changed in Version Java 8+ Thnks for the hint