Path: csiph.com!eternal-september.org!feeder.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail From: Dr J R Stockton Newsgroups: comp.lang.java.programmer Subject: Re: Parsing calendar weeks to LocalDates with DateTimeFormatter Date: Tue, 13 Sep 2016 23:39:53 +0100 Organization: Home Lines: 47 Message-ID: References: <87inu8gpqv.fsf@gnu.org> <8760q1fuw4.fsf@gnu.org> Mime-Version: 1.0 Content-Type: text/plain;charset=iso-8859-1 Content-Transfer-Encoding: 8bit Injection-Info: mx02.eternal-september.org; posting-host="01da9e6251d9b3ee47bb70bebae42d53"; logging-data="1833"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/GUXavPMQTOynQWGazALF6ojkPeotzZRw=" User-Agent: Turnpike/6.05-S () Cancel-Lock: sha1:+yvG7aQ3dAVrxfP0cPuq9SqtZNw= Xref: csiph.com comp.lang.java.programmer:35592 In comp.lang.java.programmer message <8760q1fuw4.fsf@gnu.org>, Mon, 12 Sep 2016 09:18:51, Tassilo Horn posted: >No problem in not being Java, and it's definitely nice how to do these >calculations "by hand". Yet my question was if there was some standard >way to come from a calendar week (ISO 8601) to a LocalDate other than >the snippet I've posted. Well, clearly one cannot properly convert a Week to a Day! This VBscript should be easy, I guess, to convert to Java, and writes "2016-09-13" which is correct. function YearMonthDay(YWD) '' ISO 8601 dim Y, M, D, FiM, SoY Y = Left(YWD, 4) : W = Mid(YWD, 7, 2) : D = Right(YWD, 1) SoY = DateSerial(Y, 1, 1) FiM = SoY + 3 - (SoY+1) mod 7 YearMonthDay = FiM + 7*(W-1) + D-1 end function document.write(YearMonthDay("2016-W37-2")) The function is well-tested, and in the page I cited. SoY stands for Start of (Gregorian) Year, and I think FiM stands for First Monday. FiM will be in the range SoY-3 to SoY+3. Adjustment will probably be needed if the zero dates of VBS & Java are not the same day of the week. YearMonthDay returns a VBS CDate, which I suppose to be similar to a Java LocalDate. Note : in VBS, there is a standard, Microsoft-documented way using DatePart to get an ISO 8601 week number from a Date (and there might be an inverse documented). There is a minor problem, IIRC, that it does not return the year number; and a major one that it gives an incorrect result for three days per 28 years. If they do have an inverse, I would not trust it until verified for at least every day in 400 years. -- (c) John Stockton, Surrey, UK. ¬@merlyn.demon.co.uk Turnpike v6.05 MIME. Merlyn Web Site < > - FAQish topics, acronyms, & links.