1

How to get the fiscal year end date (2017-09-30) dynamically. Based on the year I need to get the fiscal year end date dynamically.

For example:

If 2017 then output should be 2017-09-30 If 2018 then output should be 2018-09-30 and so on.

Code:

 Calendar.getInstance().getActualMaximum(Calendar.SEPTEMBER);

Output I am getting as "4"

Can I know how to get the end date dynamically.

bharathi
  • 6,019
  • 23
  • 90
  • 152
  • The solution is a simple String concatenation. The only dynamic property is the year. The rest is always the same: getEndOfSept(String year){ return year + "-09-30"; } – Stimpson Cat Feb 20 '17 at 07:52
  • 2
    Aside from anything else, do you *have* to keep using the horrible Calendar API? If you can use Joda Time or java.time you'll end up with far fewer horrible hacks. – Jon Skeet Feb 20 '17 at 07:54

2 Answers2

0

This will help you

private static String getDate(int month, int year) {
    Calendar calendar = Calendar.getInstance();
    // passing month-1 because 0-->jan, 1-->feb... 11-->dec
    calendar.set(year, month - 1, 1);
    calendar.set(Calendar.DATE, calendar.getActualMaximum(Calendar.DATE));
    Date date = calendar.getTime();
    DateFormat DATE_FORMAT = new SimpleDateFormat("MM/dd/yyyy");
    return DATE_FORMAT.format(date);
}
Nidhi257
  • 754
  • 1
  • 5
  • 23
  • FYI, the troublesome old date-time classes such as [`java.util.Date`](https://docs.oracle.com/javase/8/docs/api/java/util/Date.html), [`java.util.Calendar`](https://docs.oracle.com/javase/8/docs/api/java/util/Date.html), and `java.text.SimpleTextFormat` are now [legacy](https://en.wikipedia.org/wiki/Legacy_system), supplanted by the [java.time](https://docs.oracle.com/javase/8/docs/api/java/time/package-summary.html) classes. – Basil Bourque Feb 20 '17 at 07:55
  • I am not going to pass any input. It need to pick it dynamically – bharathi Feb 20 '17 at 07:59
  • dynamically pass values and get result then. – xxlali Feb 20 '17 at 08:01
  • if you want to find only of September you can hard code month=9 or you can iterate it for each month. – Nidhi257 Feb 20 '17 at 08:04
  • Based on year, I have to get the September month. Date and month remains constant – bharathi Feb 20 '17 at 08:09
  • @Nidhi257 Except, whoops, the `Calendar` class has crazy numbering of months where `9` is *not* September. Use java.time classes instead, specifically [`Month.SEPTEMBER`](https://docs.oracle.com/javase/8/docs/api/java/time/Month.html#SEPTEMBER). – Basil Bourque Feb 20 '17 at 08:13
0

Fiscal year?

The term “fiscal year” is not defined here. Usually that is a company-specific definition, and may not be as simple as “end of September”.

Avoid legacy date-time classes

If you are asking for the same month and same day-of-month but adjusted by year, use the java.time classes. The troublesome Calendar class is now legacy, and should be avoided.

LocalDate & MonthDay

The LocalDate class represents a date-only value without time-of-day and without time zone.

The MonthDay class represents a month and day-of-month without any year and without any time zone.

LocalDate ld = LocalDate.of( 2017 , Month.SEPTEMBER , 30 );
MonthDay md = MonthDay.from( ld );
LocalDate ldInAnotherYear = md.atYear( ld.getYear() + 1 );

You could also simply add a year depending on the date in question and what your desired behavior is for handling the last days of a month since months have different lengths, and of course February has the issue of Leap Year.

LocalDate yearLater = LocalDate.of( 2017 , Month.SEPTEMBER , 30 ).plusYears( 1 );

YearMonth

You might find the YearMonth class handy. You can ask it for the last day of the month.

YearMonth ym = YearMonth( 2017 , Month.SEPTEMBER );
LocalDate endOfMonth = ym.atEndOfMonth();

I recommend using objects rather than mere integers. Consider passing around objects such as YearMonth, MonthDay, LocalDate, and Month where appropriate rather than numbers.


About java.time

The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date, Calendar, & SimpleDateFormat.

The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.

To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.

Where to obtain the java.time classes?

The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval, YearWeek, YearQuarter, and more.

Community
  • 1
  • 1
Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154