November 24, 201015 yr Would anyone know why the following code works correctly on Windows but not on Mac??? The CORRECT answer for today (24/11/2010) is 47, not 48 as returned on macOS, as according to ISO8601 Same on 10.5.8 and 10.6.4 // DKSwebWeek ( fm_date ) // 11_11_24 JR // v1.1 // returns filenames for current week of diary // also prev && end to insert into html import java.text.SimpleDateFormat def lPad = {it -> st = '00' + it.toString() return st.substring(st.length()-2, st.length()) } dfm = new SimpleDateFormat("dd/MM/yyyy") cal=Calendar.getInstance() cal.setTime( dfm.parse(fm_date) ) now = cal.get(Calendar.WEEK_OF_YEAR) cal.add(Calendar.DAY_OF_MONTH,-7) prev = cal.get(Calendar.WEEK_OF_YEAR) cal.add(Calendar.DAY_OF_MONTH,14) next = cal.get(Calendar.WEEK_OF_YEAR) prev = 'diary' + lPad(prev) + '.shtml' next = 'diary' + lPad(next) + '.shtml' return 'diary' + lPad(now) + '.shtml' Edited November 25, 201015 yr by Guest
November 25, 201015 yr Author No further on with why the get(Calendar.WEEK_OF_YEAR) returns differently on Mac and PC But Tim Yates at StackOverflow provided a bit of a grooviness to this, which does work correctly // DKSwebWeek ( fm_date ) // 11_11_24 JR // v1.3 // returns filenames for current week of diary // also prev && end to insert into html Calendar.getInstance().with { cal -> // We want ISO Week numbers cal.firstDayOfWeek = MONDAY cal.minimalDaysInFirstWeek = 4 cal.setTime(Date.parse('dd/MM/yyyy', fm_date )) //following line will only work in later groovy version //now = cal[ WEEK_OF_YEAR ] now = cal.get(Calendar.WEEK_OF_YEAR) cal.add(Calendar.DAY_OF_MONTH,-7) prev = cal.get(Calendar.WEEK_OF_YEAR) cal.add(Calendar.DAY_OF_MONTH,14) next = cal.get(Calendar.WEEK_OF_YEAR) } prev = "diary${"$prev".padLeft( 2, '0' )}.shtml" next = "diary${"$next".padLeft( 2, '0' )}.shtml" return "diary${"$now".padLeft( 2, '0' )}.shtml" Edited November 25, 201015 yr by Guest
Create an account or sign in to comment