views:

52

answers:

2

I have a variable in my Java class that needs to be set based on whether today is before or after 7/1. If today is before 7/1 then we are in fiscal year that is the current year (so today we are in FY10). If today is after 7/1 our new fiscal year has started and the variable needs to be the next year (so FY11).

psuedo code:

if today < 7/1/anyyear then
  BudgetCode = "1" + thisYear(YY)  //variable will be 110
else
  BudgetCode = "1" + nextYear(YY)  //variable will be 111

thanks!

+3  A: 
Calendar cal = Calendar.getInstance();
cal.set(Calendar.MONTH, Calendar.JULY);
cal.set(Calendar.DATE, 1);

if (cal.after(someDate)) {
  fy = cal.get(Calendar.YEAR) + 1;
}
else {
  fy = cal.get(Calendar.YEAR);
}
mamboking
is there a way to only get the 2 digit year and not the 4 digit year? I just need 10 or 11 not 2010 or 2011, thanks
Leslie
One way would be the modulus operator, e.g.: int twoDigitFy = fy % 100;
NobodyMan
A: 

I think the if statement would be like this to get the 2 digit year with the "1" digit on the front end of it.

if (cal.after(someDate)) {
  BudgetCode = "1".concat(new Integer(cal.get(Calendar.YEAR)%100).toString());
}
else {
  BudgetCode = "1".concat(new Integer(cal.get(Calendar.YEAR)%100).toString());
}
ProfessionalAmateur
perfect! thanks!
Leslie