»
TMO Talk » Life » What does this bit of code do - What's the %1000 ?
Author
Topic: What does this bit of code do - What's the %1000 ?
Darryn.R
TMO Admin
posted 08.01.2010 07:52
(It's to create an order ID, but they're coming in too long now it's 2010.. int iYYYY = cal.get(java.util.Calendar.YEAR); int iY = (iYYYY % 1000); sY = Integer.toString(iY, 10); Full string: { String sOrderID = null; GregorianCalendar cal = new GregorianCalendar(); String sY = null; String sDDD = null; String sHH = null; String smm = null; String sSS = null; int iYYYY = cal.get(java.util.Calendar.YEAR); int iY = (iYYYY % 1000); sY = Integer.toString(iY, 10); int iDDD = cal.get(java.util.Calendar.DAY_OF_YEAR); sDDD = Integer.toString(iDDD, 10); sDDD = fitString(sDDD, 3, PAD_LEFT, '0'); int iHH = cal.get(java.util.Calendar.HOUR_OF_DAY); sHH = Integer.toString(iHH, 10); sHH = fitString(sHH, 2, PAD_LEFT, '0'); int imm = cal.get(java.util.Calendar.MINUTE); smm = Integer.toString(imm, 10); smm = fitString(smm, 2, PAD_LEFT, '0'); int iSS = cal.get(java.util.Calendar.SECOND); sSS = Integer.toString(iSS, 10); sSS = fitString(sSS, 2, PAD_LEFT, '0'); sOrderID = sY + sDDD + sHH + smm + sSS; return sOrderID; [ 08.01.2010, 07:53: Message edited by: Darryn.R ] -------------------- my own brother a god dam shit sucking vampire!!! you wait till mum finds out buddy!
Posts: 6961
| IP: Logged
dang65
it's all the rage
posted 08.01.2010 08:12
I don't know Java, but it looks like that's to do with adding a "0" pad for the last ten years (2000-2009). The % is a "remainder operator", so it's dividing the year by 1000 and returning the remainder - e.g. 9 last year. I've lost track a bit after that, but it's something along those lines, where it would change "9" to "09" because you're only using the two-digit year reference instead of the whole "2009".
Posts: 8467
| IP: Logged
dang65
it's all the rage
posted 08.01.2010 08:30
Actually, I can't see where the year number is being padded, so maybe it isn't? It's creating the OrderID with the first digit, or digits, being the year once you've dumped the thousands. So the last ten years would have been one digit, and now it's two digits. Which is presumably making your OrderID too long? The problem was created when the code was first written and didn't pad the year. If I've got that right.
Posts: 8467
| IP: Logged
Darryn.R
TMO Admin
posted 08.01.2010 08:55
Bingo - That's it - Thanks Dang. I couldnb't get my head round the "remainder operator" - The bloody years not padded, so 10 is one digit too long. -------------------- my own brother a god dam shit sucking vampire!!! you wait till mum finds out buddy!
Posts: 6961
| IP: Logged
Contact Us | The Moon Online
copyright TMO y2k+
Powered by Infopop Corporation
UBB.classic™
6.6.1