检查变量值的日期

时间:2010-03-31 15:53:10

标签: java date comparison

我的Java类中有一个变量需要根据今天是在7/1之前还是之后设置。如果今天是在7/1之前,那么我们在当年的财政年度(今天我们在2010财年)。如果今天是在7/1之后,我们的新财政年度已经开始,变量需要在下一年(因此,FY11)。

psuedo代码:

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

谢谢!

2 个答案:

答案 0 :(得分:3)

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);
}

答案 1 :(得分:0)

我认为if语句就是这样,可以得到前端有“1”数字的2位数年份。

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());
}