Skip to content
Advertisement

Use DateTimeFormatterBuilder for parsing dates of missing day and default to end of month

I need to parse dates in various formats. Some of these are missing the “day”. Now I want to default to end of month. I am not sure if there is a direct way where we can default to end of month

JavaScript

But if not, how can I know that the original date was missing the day (or that a default was used) and therefore I need to adjust it manually by using LocalDate.parse("11.2017", f).with(TemporalAdjusters.lastDayOfMonth()).

Advertisement

Answer

JavaScript

Just set DAY_OF_MONTH with 31, DAY_OF_MONTH will base on rangeUnit to choose the last day of parse month.

JavaScript

Example:

JavaScript

Output:

28

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement