Reputation: 779
I am trying to find out current financial year (FY - March to April) on the basis of current date in more efficient way. Here's what I have written so far
public static void main(String[] args) {
int year = getYearFromDate(new Date());
System.out.println("Financial Year : " + year + "-" + (year+1));
System.out.println("Financial month : " + getMonthFromDate(new Date()));
}
private static int getMonthFromDate(Date date) {
int result = -1;
if (date != null) {
Calendar cal = Calendar.getInstance();
cal.setTime(date);
result = cal.get(Calendar.MONTH)+1;
}
return result;
}
public static int getYearFromDate(Date date) {
int result = -1;
if (date != null) {
Calendar cal = Calendar.getInstance();
cal.setTime(date);
result = cal.get(Calendar.YEAR);
}
return result;
}
So if the current month is less than or equal to 3 (March) and year is 2013, FY should be = 2012-2013, if the month is 6(June) and year is 2013, FY should be = 2013-2014.
How do I achieve it?
Upvotes: 2
Views: 51804
Reputation: 3924
public String getFinancialYear() {
String finYear;
int year = Calendar.getInstance().get(Calendar.YEAR);
int month = Calendar.getInstance().get(Calendar.MONTH) + 1;
if (month <= 3) {
finYear = year - 1 + "-" + year;
} else {
finYear = year + "-" + (year + 1);
}
return finYear;
}
Upvotes: 0
Reputation: 119
public static int getCurrentFromFinancialYear(String inputDate) {
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd-MM-yyyy HH:mm:ss:SSS");
LocalDate todayDateTime = LocalDate.parse(inputDate, formatter);
int year = todayDateTime.getYear();
String fiscalDateStr = "01-04-" + year + " 00:00:00:000";
LocalDate fiscalDateTime = LocalDate.parse(fiscalDateStr, formatter);
if (todayDateTime.isBefore(fiscalDateTime)) {
year = year - 1;
}
return year;
}
Upvotes: 0
Reputation: 11
Simple solution to find given date financial year in Java
package testing;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.ZoneId;
import java.util.Date;
public class CurrentFinancialYear {
public static void main(String[] args) throws ParseException {
System.out.println(getActualFinancialYear("01-02-2022 23:10:10"));
}
public static int getActualFinancialYear(String givendate) throws ParseException {
SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");
Date givenDate=formatter.parse(givendate);
LocalDate localDate1 = givenDate.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
int year = localDate1.getYear();
String fiscaldate = "31-03-" + year + " 23:59:59";
Date fiscalDate = formatter.parse(fiscaldate);
if (givenDate.before(fiscalDate)) {
year = year - 1;
}
return year;
}
}
Upvotes: 1
Reputation: 11
Javascript
let FY = document.getElementById("fYear");
FY.innerHTML = new Date().getMonth() <= 9 ? "Fiscal Year "+new Date().getFullYear() : "Fiscal Year "+new Date().getFullYear() +1;
Upvotes: 1
Reputation: 61
int CurrentYear = Calendar.getInstance().get(Calendar.YEAR);
int CurrentMonth = (Calendar.getInstance().get(Calendar.MONTH)+1);
String financiyalYearFrom="";
String financiyalYearTo="";
if (CurrentMonth<4) {
financiyalYearFrom="01-04-"+(CurrentYear-1);
financiyalYearTo="31-03-"+(CurrentYear);
} else {
financiyalYearFrom="01-04-"+(CurrentYear);
financiyalYearTo="31-03-"+(CurrentYear+1);
}
Upvotes: 5
Reputation: 1
This code allows you to set an anniversary date and set the fiscal year on different constructors with the given assumptions.
public class FiscalYear {
public String fyAnnualStartDate = "02-01";
public Calendar fyCalStart;
public Calendar fyCalEnd;
public Date fyStartDate;
public Date fyEndDate;
public int year;
public int month;
public int day;
public String timeZone = "PST";
protected SimpleDateFormat isoSDF = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
public Date statusTime = null;
public String stringTime = "";
public String fyName;
/**
* This is our lazy constructor assumes this is 02-01 for the start year and PST for the time zone
*
* Assumes that the year passed is the start of the year which assigns to the fiscal year
*
* I.E. 2020 would be fy21
*
* @param year
* @throws ParseException
*/
public FiscalYear(int year) throws ParseException {
// TODO Auto-generated constructor stub
this.year = year;
setYearMonth();
setDates();
setName();
}
public FiscalYear(int year, String zone) throws ParseException {
// TODO Auto-generated constructor stub
this.year = year;
this.timeZone = zone;
setYearMonth();
setDates();
setName();
}
/**
* This constructor allows you to set the annualstartdate if you need to adjust the fiscal year to a new company
* @param year
* @param zone
* @param annualStartDate
* @throws ParseException
*/
public FiscalYear(int year, String zone, String annualStartDate) throws ParseException {
// TODO Auto-generated constructor stub
this.fyAnnualStartDate = annualStartDate;
this.year = year;
this.timeZone = zone;
setYearMonth();
setDates();
setName();
}
/**
* This constructor allows you to set the annualstartdate if you need to adjust the fiscal year to a new company
* However this allows you to provide the current date or date in general to confirm the true fiscal year
* @param year
* @param zone
* @param annualStartDate
* @throws Exception
*/
public FiscalYear(Date year, String zone, String annualStartDate) throws Exception {
// TODO Auto-generated constructor stub
this.timeZone = zone;
this.fyAnnualStartDate = annualStartDate;
this.year = getActualYear(year);
setYearMonth();
setDates();
setName();
}
/**
* This constructor allows you to provide the current date or date in general to confirm the true fiscal year
* Default annual start date is set in var fyAnnualStartDate
*
* @param year
* @throws Exception
*/
public FiscalYear(Date year) throws Exception {
// TODO Auto-generated constructor stub
this.year = getActualYear(year);
setYearMonth();
setDates();
setName();
}
/**
* Gets the actual FY to be correct given the window can a Fiscal year can have two years within it
*
* This tests to figure out which FY this should instantiate to given the date you are providing based on the start date and logic that
* Fiscal Year 21 corresponds to the a lesser actual start year
* I.E. 2020 would be fy21
* @param year2
* @return
* @throws Exception
*/
public static int getActualYear(Date year2) throws Exception {
// TODO Auto-generated method stub
SimpleDateFormat isoSDFTest = new SimpleDateFormat("yyyy");
FiscalYear test = new FiscalYear(Integer.parseInt(isoSDFTest.format(year2)));
FiscalYear testBefore = new FiscalYear(Integer.parseInt(isoSDFTest.format(year2))-1);
FiscalYear testAfter = new FiscalYear(Integer.parseInt(isoSDFTest.format(year2))+1);
if ((test.getStartDateAsDate().before(year2) && test.getEndDateAsDate().after(year2)) || year2.equals(test.getStartDateAsDate()) || year2.equals(test.getEndDateAsDate())) {
return Integer.parseInt(isoSDFTest.format(test.getStartDateAsDate()));
}
if ((testBefore.getStartDateAsDate().before(year2) && testBefore.getEndDateAsDate().after(year2)) || year2.equals(testBefore.getStartDateAsDate()) || year2.equals(testBefore.getEndDateAsDate())) {
return Integer.parseInt(isoSDFTest.format(testBefore.getStartDateAsDate()));
}
if ((testAfter.getStartDateAsDate().before(year2) && testAfter.getEndDateAsDate().after(year2)) || year2.equals(testAfter.getStartDateAsDate()) || year2.equals(testAfter.getEndDateAsDate())) {
return Integer.parseInt(isoSDFTest.format(testAfter.getStartDateAsDate()));
}
throw new Exception("Issue figuring window out");
}
private void setName() {
// TODO Auto-generated method stub
String fy = "FY";
this.fyName = fy+String.valueOf(year+1).substring(2,4);
}
private void setYearMonth() {
// TODO Auto-generated method stub
String [] arr = fyAnnualStartDate.split("-");
month = Integer.parseInt(arr[0])-1;
day = Integer.parseInt(arr[1]);
}
private void setDates() throws ParseException {
// TODO Auto-generated method stub
isoSDF.setTimeZone(TimeZone.getTimeZone(timeZone));
Calendar cal = Calendar.getInstance();
cal.setTimeZone(TimeZone.getTimeZone(timeZone));
cal.set(Calendar.YEAR, year);
cal.set(Calendar.MONTH, month);
cal.set(Calendar.DAY_OF_MONTH, day);
cal.set(Calendar.HOUR_OF_DAY,0);
cal.set(Calendar.MINUTE,0);
cal.set(Calendar.SECOND,0);
cal.set(Calendar.MILLISECOND,0);
fyCalStart = cal;
fyStartDate = cal.getTime();
Calendar endCal = (Calendar) cal.clone();
endCal.add(Calendar.YEAR, 1);
endCal.add(Calendar.SECOND,-1);
fyCalEnd = endCal;
fyEndDate = endCal.getTime();
statusTime = isoSDF.parse(isoSDF.format(new Date()));
stringTime = isoSDF.format(statusTime);
}
public String getStartDate() {
return isoSDF.format(fyStartDate);
}
public String getEndDate() {
return isoSDF.format(fyEndDate);
}
public Date getStartDateAsDate() {
return fyStartDate;
}
public Date getEndDateAsDate() {
return fyEndDate;
}
public void shiftWindow(int shift) {
fyCalStart.add(Calendar.YEAR, shift);
fyCalEnd.add(Calendar.YEAR, shift);
fyStartDate = fyCalStart.getTime();
fyEndDate = fyCalEnd.getTime();
year = year + shift;
setName();
}
public String getQuarterStartDate(int quarter) throws Exception {
Calendar temp = (Calendar) fyCalStart.clone();
quarter = validateQuarter(quarter);
if (quarter==1) {
} else if (quarter==2) {
temp.add(Calendar.MONTH, 3);
} else if (quarter==3) {
temp.add(Calendar.MONTH, 6);
} else {
temp.add(Calendar.MONTH, 9);
}
Date tempDate = temp.getTime();
return isoSDF.format(tempDate);
}
public String getQuarterEndDate(int quarter) throws Exception {
Calendar temp = (Calendar) fyCalEnd.clone();
quarter = validateQuarter(quarter);
if (quarter==1) {
temp.add(Calendar.MONTH, -9);
} else if (quarter==2) {
temp.add(Calendar.MONTH, -6);
} else if (quarter==3) {
temp.add(Calendar.MONTH, -3);
} else {
}
Date tempDate = temp.getTime();
return isoSDF.format(tempDate);
}
public String getMonthStartDate(int quarter, int month) throws Exception {
Calendar temp = (Calendar) fyCalStart.clone();
quarter = validateQuarter(quarter);
month = validateMonth(month);
if (quarter==1) {
if (month==1) {
} else if (month==2) {
temp.add(Calendar.MONTH, 1);
} else {
temp.add(Calendar.MONTH, 2);
}
} else if (quarter==2) {
if (month==1) {
temp.add(Calendar.MONTH, 3);
} else if (month==2) {
temp.add(Calendar.MONTH, 4);
} else {
temp.add(Calendar.MONTH, 5);
}
} else if (quarter==3) {
if (month==1) {
temp.add(Calendar.MONTH, 6);
} else if (month==2) {
temp.add(Calendar.MONTH, 7);
} else {
temp.add(Calendar.MONTH, 8);
}
} else {
if (month==1) {
temp.add(Calendar.MONTH, 9);
} else if (month==2) {
temp.add(Calendar.MONTH, 10);
} else {
temp.add(Calendar.MONTH, 11);
}
}
Date tempDate = temp.getTime();
return isoSDF.format(tempDate);
}
public String getMonthEndDate(int quarter, int month) throws Exception {
Calendar temp = (Calendar) fyCalEnd.clone();
quarter = validateQuarter(quarter);
month = validateMonth(month);
if (quarter==1) {
if (month==1) {
temp.add(Calendar.MONTH, -11);
} else if (month==2) {
temp.add(Calendar.MONTH, -10);
} else {
temp.add(Calendar.MONTH, -9);
}
} else if (quarter==2) {
if (month==1) {
temp.add(Calendar.MONTH, -8);
} else if (month==2) {
temp.add(Calendar.MONTH, -7);
} else {
temp.add(Calendar.MONTH, -6);
}
} else if (quarter==3) {
if (month==1) {
temp.add(Calendar.MONTH, -5);
} else if (month==2) {
temp.add(Calendar.MONTH, -4);
} else {
temp.add(Calendar.MONTH, -3);
}
} else {
if (month==1) {
temp.add(Calendar.MONTH, -2);
} else if (month==2) {
temp.add(Calendar.MONTH, -1);
} else {
}
}
Date tempDate = temp.getTime();
return isoSDF.format(tempDate);
}
public Date getQuarterStartDateAsDate(int quarter) throws Exception {
Calendar temp = (Calendar) fyCalStart.clone();
quarter = validateQuarter(quarter);
if (quarter==1) {
} else if (quarter==2) {
temp.add(Calendar.MONTH, 3);
} else if (quarter==3) {
temp.add(Calendar.MONTH, 6);
} else {
temp.add(Calendar.MONTH, 9);
}
Date tempDate = temp.getTime();
return tempDate;
}
public Date getQuarterEndDateAsDate(int quarter) throws Exception {
Calendar temp = (Calendar) fyCalEnd.clone();
quarter = validateQuarter(quarter);
if (quarter==1) {
temp.add(Calendar.MONTH, -9);
} else if (quarter==2) {
temp.add(Calendar.MONTH, -6);
} else if (quarter==3) {
temp.add(Calendar.MONTH, -3);
} else {
}
Date tempDate = temp.getTime();
return tempDate;
}
public Date getMonthStartDateAsDate(int quarter, int month) throws Exception {
Calendar temp = (Calendar) fyCalStart.clone();
quarter = validateQuarter(quarter);
month = validateMonth(month);
if (quarter==1) {
if (month==1) {
} else if (month==2) {
temp.add(Calendar.MONTH, 1);
} else {
temp.add(Calendar.MONTH, 2);
}
} else if (quarter==2) {
if (month==1) {
temp.add(Calendar.MONTH, 3);
} else if (month==2) {
temp.add(Calendar.MONTH, 4);
} else {
temp.add(Calendar.MONTH, 5);
}
} else if (quarter==3) {
if (month==1) {
temp.add(Calendar.MONTH, 6);
} else if (month==2) {
temp.add(Calendar.MONTH, 7);
} else {
temp.add(Calendar.MONTH, 8);
}
} else {
if (month==1) {
temp.add(Calendar.MONTH, 9);
} else if (month==2) {
temp.add(Calendar.MONTH, 10);
} else {
temp.add(Calendar.MONTH, 11);
}
}
Date tempDate = temp.getTime();
return tempDate;
}
public Date getMonthEndDateAsDate(int quarter, int month) throws Exception {
Calendar temp = (Calendar) fyCalEnd.clone();
quarter = validateQuarter(quarter);
month = validateMonth(month);
if (quarter==1) {
if (month==1) {
temp.add(Calendar.MONTH, -11);
} else if (month==2) {
temp.add(Calendar.MONTH, -10);
} else {
temp.add(Calendar.MONTH, -9);
}
} else if (quarter==2) {
if (month==1) {
temp.add(Calendar.MONTH, -8);
} else if (month==2) {
temp.add(Calendar.MONTH, -7);
} else {
temp.add(Calendar.MONTH, -6);
}
} else if (quarter==3) {
if (month==1) {
temp.add(Calendar.MONTH, -5);
} else if (month==2) {
temp.add(Calendar.MONTH, -4);
} else {
temp.add(Calendar.MONTH, -3);
}
} else {
if (month==1) {
temp.add(Calendar.MONTH, -2);
} else if (month==2) {
temp.add(Calendar.MONTH, -1);
} else {
}
}
Date tempDate = temp.getTime();
return tempDate;
}
/**
* Gets the current FY year based on start year
* @return
*/
public String getFYName() {
return fyName;
}
/**
* Gets a systematic name for the FY year plus quarter
* @param quarter
* @return
* @throws Exception
*/
public String getQuarterName(int quarter) throws Exception {
quarter=validateQuarter(quarter);
return fyName+".Q"+quarter;
}
/**
* Gets a systematic name for the FY year, quarter and month
* @param quarter
* @param month
* @return
* @throws Exception
*/
public String getMonthName(int quarter, int month) throws Exception {
quarter=validateQuarter(quarter);
month=validateMonth(month);
return fyName+".Q"+quarter+".M"+month;
}
public int validateQuarter(int quarter) throws Exception {
int [] test = {1,2,3,4};
for(int i:test) {
if (i==quarter) {
return i;
}
}
throw new Exception("Invalid Quarter");
}
public int validateMonth(int month) throws Exception {
int [] test = {1,2,3};
for(int i:test) {
if (i==month) {
return i;
}
}
throw new Exception("Invalid Month");
}
/**
* Method tests if given a string format does it belong to the current fiscalyear
* Does not say if its a future or past just returns true or false
* @param fy
* @return
* @throws Exception
*/
public static boolean inCurrentFyWindow(String fy) throws Exception {
Date yearTest = new Date();
FiscalYear test = new FiscalYear(yearTest);
if (fy.length()==8 ||fy.length()==6) {
//FY21Q1M1
String fyName = fy.split("Q")[0];
if (test.getFYName().toLowerCase().equals(fyName.toLowerCase())) {
return true;
} else {
return false;
}
} else {
//FY21
if (test.getFYName().toLowerCase().equals(fy.toLowerCase())) {
return true;
} else {
return false;
}
}
}
/**
* Method tests if the string for FiscalYear being passed has passed or not based on the current FiscalYear
*
* This can test FY year, FY year, quarter and finally FY year, quarter and month combinations
*
* @param fy
* @return
* @throws Exception
*/
public static boolean hasWindowHappenedFully(String fy) throws Exception {
Date yearTest = new Date();
if (fy.length()==8) {
//FY21Q1M1
String fyName = "20" + fy.substring(2, 4);
int quarter = Integer.parseInt(fy.substring(5, 6));
int month = Integer.parseInt(fy.substring(7, 8));
FiscalYear yQM = new FiscalYear(Integer.parseInt(fyName)-1);
if (yearTest.after(yQM.getMonthEndDateAsDate(quarter, month))) {
return true;
} else {
return false;
}
} else if (fy.length()==6) {
//FY21Q1
String fyName = "20" + fy.substring(2, 4);
int quarter = Integer.parseInt(fy.substring(5, 6));
FiscalYear yQM = new FiscalYear(Integer.parseInt(fyName)-1);
if (yearTest.after(yQM.getQuarterEndDateAsDate(quarter))) {
return true;
} else {
return false;
}
} else {
//FY21
String fyName = "20" + fy.substring(2, 4);
FiscalYear yQM = new FiscalYear(Integer.parseInt(fyName)-1);
if (yearTest.after(yQM.getEndDateAsDate())) {
return true;
} else {
return false;
}
}
}
public static String getCurrentFiscalYear() throws Exception {
Date yearTest = new Date();
FiscalYear test = new FiscalYear(yearTest);
return test.getFYName();
}
public static String getCurrentFiscalYearQuarter() throws Exception {
Date yearTest = new Date();
FiscalYear test = new FiscalYear(yearTest);
for(int q=1; q<=4; q++ ) {
for(int m=1; m<=3; m++ )
if (test.getMonthStartDateAsDate(q, m).before(yearTest) && test.getMonthEndDateAsDate(q, m).after(yearTest)) {
return test.getFYName()+".Q"+q;
}
}
return test.getFYName();
}
public static String getCurrentFiscalYearQuarterMonth() throws Exception {
Date yearTest = new Date();
FiscalYear test = new FiscalYear(yearTest);
for(int q=1; q<=4; q++ ) {
for(int m=1; m<=3; m++ )
if (test.getMonthStartDateAsDate(q, m).before(yearTest) && test.getMonthEndDateAsDate(q, m).after(yearTest)) {
return test.getFYName()+".Q"+q+".M"+m;
}
}
return test.getFYName();
}
public static String getFiscalYear(Date year2) throws Exception {
FiscalYear test = new FiscalYear(year2);
return test.getFYName();
}
public static String getFiscalYearQuarter(Date year2) throws Exception {
FiscalYear test = new FiscalYear(year2);
for(int q=1; q<=4; q++ ) {
for(int m=1; m<=3; m++ )
if (test.getMonthStartDateAsDate(q, m).before(year2) && test.getMonthEndDateAsDate(q, m).after(year2) || year2.equals(test.getMonthStartDateAsDate(q, m)) || year2.equals(test.getMonthEndDateAsDate(q, m))) {
return test.getFYName()+".Q"+q+".M"+m;
}
}
return test.getFYName();
}
public static String getFiscalYearQuarterMonth(Date year2) throws Exception {
FiscalYear test = new FiscalYear(year2);
for(int q=1; q<=4; q++ ) {
for(int m=1; m<=3; m++ )
if (test.getMonthStartDateAsDate(q, m).before(year2) && test.getMonthEndDateAsDate(q, m).after(year2) || year2.equals(test.getMonthStartDateAsDate(q, m)) || year2.equals(test.getMonthEndDateAsDate(q, m))) {
return test.getFYName()+".Q"+q+".M"+m;
}
}
return test.getFYName();
}
}
Upvotes: 0
Reputation: 1280
For those who are looking for a solution in JavaScript, here it is using MomentJS library.
let financialYear;
let today = moment();
if(today.month() >= 3){
financialYear = today.format('YYYY') + '-' + today.add(1, 'years').format('YYYY')
}
else{
financialYear = today.subtract(1, 'years').format('YYYY') + '-' + today.add(1, 'years').format('YYYY')
}
console.log(financialYear)
https://codepen.io/connect_dips/pen/QWWdwed
Upvotes: 0
Reputation: 1
public interface FinancialYearService {
public int getFinancialYear();
public int getFinancialMonth();
public void generateFinancialYearMonth(int month,int year);
}
public class FinancialYearServiceImpl implements FinancialYearService {
private int financialYear;
private int financialMonth;
private void setFinancialYear(int financialYear) {
this.financialYear = financialYear;
}
private void setFinancialMonth(int financialMonth) {
this.financialMonth = financialMonth;
}
@Override
public int getFinancialYear() {
return financialYear;
}
@Override
public int getFinancialMonth() {
return financialMonth;
}
/*
* (non-Javadoc)
* have to send current month and year to get financial month and year
*/
@Override
public void generateFinancialYearMonth(int month,int year) {
if(month <= 3 ){
setFinancialMonth(month + 9);
setFinancialYear(year - 1);
}
else{
setFinancialMonth(month - 3);
setFinancialYear(year);
}
}
}
Upvotes: 0
Reputation: 51553
I suspect that one of the values needed is the fiscal month. The fiscal month is the month within the fiscal year. For instance, if the fiscal year starts in March, then March is the 0 month of the fiscal year. February is the 11 month of the fiscal year.
Here are some test results:
Current Date : Wed Sep 04 14:23:17 EDT 2013
Fiscal Years : 2013-2014
Fiscal Month : 6
Current Date : Fri Feb 01 00:00:00 EST 2013
Fiscal Years : 2012-2013
Fiscal Month : 11
Current Date : Wed Jul 25 00:00:00 EDT 2012
Fiscal Years : 2012-2013
Fiscal Month : 4
Borrowing from Kevin Bowersox's answer, here's a FiscalDate class that gives the fiscal year and fiscal month, as well as the calendar year and calendar month. Both month values are zero based.
import java.util.Calendar;
import java.util.Date;
public class FiscalDate {
private static final int FIRST_FISCAL_MONTH = Calendar.MARCH;
private Calendar calendarDate;
public FiscalDate(Calendar calendarDate) {
this.calendarDate = calendarDate;
}
public FiscalDate(Date date) {
this.calendarDate = Calendar.getInstance();
this.calendarDate.setTime(date);
}
public int getFiscalMonth() {
int month = calendarDate.get(Calendar.MONTH);
int result = ((month - FIRST_FISCAL_MONTH - 1) % 12) + 1;
if (result < 0) {
result += 12;
}
return result;
}
public int getFiscalYear() {
int month = calendarDate.get(Calendar.MONTH);
int year = calendarDate.get(Calendar.YEAR);
return (month >= FIRST_FISCAL_MONTH) ? year : year - 1;
}
public int getCalendarMonth() {
return calendarDate.get(Calendar.MONTH);
}
public int getCalendarYear() {
return calendarDate.get(Calendar.YEAR);
}
public static void main(String[] args) {
displayFinancialDate(Calendar.getInstance());
displayFinancialDate(setDate(2013, 1, 1));
displayFinancialDate(setDate(2012, 6, 25));
}
private static Calendar setDate(int year, int month, int day) {
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.YEAR, year);
calendar.set(Calendar.MONTH, month);
calendar.set(Calendar.DAY_OF_MONTH, day);
calendar.set(Calendar.HOUR_OF_DAY, 0);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
return calendar;
}
private static void displayFinancialDate(Calendar calendar) {
FiscalDate fiscalDate = new FiscalDate(calendar);
int year = fiscalDate.getFiscalYear();
System.out.println("Current Date : " + calendar.getTime().toString());
System.out.println("Fiscal Years : " + year + "-" + (year + 1));
System.out.println("Fiscal Month : " + fiscalDate.getFiscalMonth());
System.out.println(" ");
}
}
Upvotes: 9
Reputation: 94499
It could be beneficial to make an object for the FiscalDate
so you can reuse it throughout an application. I would avoid deprecated methods such as getMonth()
and getYear()
as others have suggested.
import java.util.Calendar;
import java.util.Date;
public class FiscalDate {
private Date actual;
private int month;
private int year;
public FiscalDate(Date date){
this.actual = date;
this.init();
}
private void init(){
Calendar cal = Calendar.getInstance();
cal.setTime(this.actual);
this.month = cal.get(Calendar.MONTH);
int advance = (this.month <= 3) ? -1:0;
this.year = cal.get(Calendar.YEAR) + advance;
}
public Date getActual() {
return actual;
}
public void setActual(Date actual) {
this.actual = actual;
}
public int getMonth() {
return month;
}
public void setMonth(int month) {
this.month = month;
}
public int getYear() {
return year;
}
public void setYear(int year) {
this.year = year;
}
public static void main(String[] args) {
FiscalDate fDate = new FiscalDate(new Date());
System.out.println(fDate.getYear());
}
}
Upvotes: 3
Reputation: 1507
public static void main(String[] args) {
int year = Calendar.getInstance().get(Calendar.YEAR);
int month = Calendar.getInstance().get(Calendar.MONTH) + 1;
System.out.println("Financial month : " + month);
if (month < 3) {
System.out.println("Financial Year : " + (year - 1) + "-" + year);
} else {
System.out.println("Financial Year : " + year + "-" + (year + 1));
}
}
Just remove the extra functions.
Upvotes: 4
Reputation: 2104
Something like:
Date d = new Date();
int y = d.getMonth() < 3 ? d.getYear() - 1 : d.getYear();
System.out.println("Financial Year : " + y + "-" + (y + 1));
System.out.println("Financial month : " + d.getMonth());
Upvotes: 2
Reputation: 40056
I wonder if you have really tried to solve it by yourself. It is so obvious and straight forward
psuedo code:
if ( monthOf(currentDate) >= MARCH) then
FY = yearOf(currentDate) + "-" + (yearOf(currentDate) +1);
else
FY = (yearOf(currentDate) - 1) + "-" + yearOf(currentDate);
Upvotes: 6