Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.java.programmer > #35560
| From | Tassilo Horn <tsdh@gnu.org> |
|---|---|
| Newsgroups | comp.lang.java.programmer |
| Subject | Parsing calendar weeks to LocalDates with DateTimeFormatter |
| Date | 2016-09-07 08:58 +0200 |
| Organization | University Koblenz-Landau Campus Koblenz |
| Message-ID | <87inu8gpqv.fsf@gnu.org> (permalink) |
Hi all,
I have a
DateTimeFormatter FORMATTER_KW = DateTimeFormatter.ofPattern("ww.YYYY");
for formatting LocalDates as calendar weeks. That works fine, however I
can't use that to parse calendar week strings to LocalDates, e.g.,
FORMATTER_KW.parse("44.2016", LocalDate::from);
throws
java.time.DateTimeException: Unable to obtain LocalDate from TemporalAccessor:
{WeekBasedYear[WeekFields[MONDAY,4]]=2016,
WeekOfWeekBasedYear[WeekFields[MONDAY,4]]=50},ISO of type java.time.format.Parsed
at java.time.LocalDate.from(LocalDate.java:368)
at java.time.format.Parsed.query(Parsed.java:226)
at java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1851)
... 63 more
Sounds like it is complaining that the date is underspecified which it
obviously is. So I'd like such a calendar week to resolve to the last
work day in that week. (The calendar weeks are estimated deliveries.)
After a bit of doc reading and experimenting, I came up with this
solution which seems to work ok but looks a bit complicated for such a
common task such as week-date conversion. So is there anything better?
// When parsing calendar weeks like "22.2016", we want to get the last work day in that week. We
// assume that everyone in every locale has a 5 days work week (don't know how to do better).
return FORMATTER_KW.parse(editorText, temporal ->
{
WeekFields weekFields = WeekFields.of(Locale.getDefault());
DayOfWeek firstDayInWeek = weekFields.getFirstDayOfWeek();
DayOfWeek lastWorkDay = firstDayInWeek.minus(4);
long weekOfWeekBasedYear = temporal.getLong(weekFields.weekOfWeekBasedYear());
long year = temporal.getLong(weekFields.weekBasedYear());
LocalDate date = LocalDate.of(0, 1, 1);
date = date.plusYears(year);
date = date.plusWeeks(weekOfWeekBasedYear);
date = date.with(lastWorkDay);
return date;
});
Bye,
Tassilo
Back to comp.lang.java.programmer | Previous | Next — Next in thread | Find similar | Unroll thread
Parsing calendar weeks to LocalDates with DateTimeFormatter Tassilo Horn <tsdh@gnu.org> - 2016-09-07 08:58 +0200
Re: Parsing calendar weeks to LocalDates with DateTimeFormatter Tassilo Horn <tsdh@gnu.org> - 2016-09-07 10:11 +0200
Re: Parsing calendar weeks to LocalDates with DateTimeFormatter Dr J R Stockton <reply1600@merlyn.demon.co.uk.invalid> - 2016-09-09 22:46 +0100
Re: Parsing calendar weeks to LocalDates with DateTimeFormatter Dr J R Stockton <reply1600@merlyn.demon.co.uk.invalid> - 2016-09-11 16:43 +0100
Re: Parsing calendar weeks to LocalDates with DateTimeFormatter Lew <lewbloch@gmail.com> - 2016-09-13 17:50 -0700
Re: Parsing calendar weeks to LocalDates with DateTimeFormatter Dr J R Stockton <reply1600@merlyn.demon.co.uk.invalid> - 2016-09-17 19:34 +0100
Re: Parsing calendar weeks to LocalDates with DateTimeFormatter Tassilo Horn <tsdh@gnu.org> - 2016-09-12 09:18 +0200
Re: Parsing calendar weeks to LocalDates with DateTimeFormatter Dr J R Stockton <reply1600@merlyn.demon.co.uk.invalid> - 2016-09-13 23:39 +0100
csiph-web