1

i'm trying to convert year and week number to Date, specifically i need to get first day of week by week number. My idea was to convert my String to Date and then gat firstdayofweek but im struggling with converting to Date with error:

Cannot coerce String (201601) to LocalDateTime, caused by: Text '201601' could not be parsed: Unable to obtain LocalDateTime from TemporalAccessor: {WeekOfWeekBasedYear[WeekFields[SUNDAY,1]]=1, Year=2016},ISO of type java.time.format.Parsed

My DW script:

%dw 2.0
output application/json
var date = '201601'
---
'Date' : date as LocalDateTime {format: "yyyyww"}

and also tried

%dw 2.0
output application/json
var date = '201601'
import * from dw::core::Periods
import * from dw::core::Dates
---
atBeginningOfWeek((atBeginningOfWeek(date[0 to 3]++ "-01-01"))  as Date + days(date [4 to -1] * 7))

But they are not working good, any ideas?

aled
  • 21,330
  • 3
  • 27
  • 34

2 Answers2

0

I think your second solution is the answer to what you are trying to achieve.

%dw 2.0
output application/json
var date = '201601'
import * from dw::core::Periods
import * from dw::core::Dates
---

atBeginningOfWeek(((date[0 to 3]) ++ "-01-01") as Date) + days (date[4 to -1]*7) 

I believe this is giving correct answer to your requirement.

StackOverflowed
  • 719
  • 3
  • 7
  • HI thanks for reply but i think it's not working properly , for 202041 start day is 2020-10-04 but script returns 2020-10-11. – Nazarii Lykholat Feb 15 '23 at 10:56
  • @NazariiLykholat not really, for 202041 , start day is 2020-10-11 , since its the start day of the 41st week of 2020. For the year 2020, the 1st week starts on 5th Jan. So by calculating accordingly the 41st week starts from 2020-10-11. – StackOverflowed Feb 15 '23 at 11:48
  • Example inputs and outputs should be provided with the question. – aled Feb 16 '23 at 03:36
0

The first script will not work because 1) using a LocalDateTime but there is no time in the input and 2) a year and a week don't make a date. DataWeave only has features to convert strings containing year, month and day into a date so even using w in the pattern will not work. Incidentally this could be the one case where the pattern YYYY (week based year) would probably apply if there was a way to use a year-week in DataWeave. Usually using uppercase Y in a date pattern in Java or DataWeave is a bug, because it is not the correct pattern for dates.

The second script seems correct, but we should try to avoid using string manipulation for dates. That's a bad pattern if there are library functions in the language used. It is not possible to avoid all because there is no way to convert a week to a date but we can at least avoid the concatenations. I prefer to implement the logic in a function to facilitate reuse.

%dw 2.0
output application/json
import * from dw::core::Dates
import * from dw::core::Periods
var yearWeekString = "201601"
fun beginningOfWeekYear(year: Number, week: Number)=
    atBeginningOfWeek(atBeginningOfWeek(date({ year: year, month: 1, day: 1})) +  days(week * 7 ))
---
{
    "201601": beginningOfWeekYear(yearWeekString[0 to 3] as Number, yearWeekString[4 to -1] as Number),
    "202041": beginningOfWeekYear(2020, 41)
}

Output:

{
  "201601": "2015-12-27",
  "202041": "2020-10-04"
}

I validated the output using a week calendar.

aled
  • 21,330
  • 3
  • 27
  • 34