Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #35592

Re: Parsing calendar weeks to LocalDates with DateTimeFormatter

From Dr J R Stockton <reply1600@merlyn.demon.co.uk.invalid>
Newsgroups comp.lang.java.programmer
Subject Re: Parsing calendar weeks to LocalDates with DateTimeFormatter
Date 2016-09-13 23:39 +0100
Organization Home
Message-ID <AHzpEb45AI2XFwXw@invalid.uk.co.demon.merlyn.invalid> (permalink)
References <87inu8gpqv.fsf@gnu.org> <XSD8BfGz2y0XFwl7@invalid.uk.co.demon.merlyn.invalid> <8760q1fuw4.fsf@gnu.org>

Show all headers | View raw


In comp.lang.java.programmer message <8760q1fuw4.fsf@gnu.org>, Mon, 12
Sep 2016 09:18:51, Tassilo Horn <tsdh@gnu.org> 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.

Back to comp.lang.java.programmer | Previous | Next — Previous in thread | Find similar | Unroll thread


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