Calculate the year quarter difference in java -
i have use case , have provide input start quarter , end quarter 2 input string. example provide 201501(start quarter) , 201602(end quarter) 2 quarter input. method give output in map previous start quarter , end quarter following previous start quarter 201303 previous end quarter 201404 ie first quarter difference between 2 time period calculated. previous last quarter 1 quarter previous input start quarter , quarter difference between 2 input derived , subtracted previous last quarter previous start quarter.
public static map<string,string> getpreviousquarter( string start,string end){ string prevstartquarter= calulatepreviousquarter(start); string prevendquarter=calulatepreviousquarter(end); map<string,string> returnmap = new hashmap<string,string>(); returnmap.put("previousstartquarter", prevstartquarter); returnmap.put("previousendquarter",prevendquarter); system.out.println("the reurnmap is" +returnmap); return returnmap; } private static string calulatepreviousquarter(string input) { int yearval=0; string year = input.substring(0,4); string quarter = input.substring(4); if(quarter.equalsignorecase("01")) { yearval = integer.valueof(year)-1; quarter ="04"; } else { yearval = integer.valueof(year); switch(quarter) { case "02": quarter ="01"; break; case "03": quarter ="02"; break; case "04": quarter ="03"; break; default: break; } } return string.valueof(yearval)+quarter; }
inputoutput input string start 201501 string end 201602 quarter difference 6 expected output map key,values previousstartquarter 201404 previousendquarter 201303
the following code should looking for:
public static map<string, string> getpreviousquarter(string start, string end) { string prevstartquarter = calulatepreviousquarter(start); string prevendquarter = calulatepreviousquarter(end); int len=1; while(!start.equals(prevendquarter)){ prevendquarter=calulatepreviousquarter(prevendquarter); len++; } string tmp=prevstartquarter; for(int i=0;i<len;i++){ tmp=calulatepreviousquarter(tmp); } map<string, string> returnmap = new hashmap<string, string>(); returnmap.put("previousstartquarter", prevstartquarter); returnmap.put("previousendquarter", tmp); system.out.println("the reurnmap is" + returnmap); return returnmap; } private static string calulatepreviousquarter(string input) { int yearval = 0; string year = input.substring(0, 4); string quarter = input.substring(4); if (quarter.equalsignorecase("01")) { yearval = integer.valueof(year) - 1; quarter = "04"; } else { yearval = integer.valueof(year); switch (quarter) { case "02": quarter = "01"; break; case "03": quarter = "02"; break; case "04": quarter = "03"; break; default: break; } } return string.valueof(yearval) + quarter; }
i hope helps.
Comments
Post a Comment