john renfrew Posted November 24, 2010 Posted November 24, 2010 (edited) 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, 2010 by Guest
john renfrew Posted November 25, 2010 Author Posted November 25, 2010 (edited) 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, 2010 by Guest
Recommended Posts
This topic is 5124 days old. Please don't post here. Open a new topic instead.
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now