I am trying to get the first date and last date of this month and year. I used the following code to get the latest date in yyyyMMdd format. But could not get this format. In addition, I want the start date to be in the same format. I am still working on it. Can someone help me in fixing the code below.
public static java.util.Date calculateMonthEndDate(int month, int year) { int[] daysInAMonth = { 29, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; int day = daysInAMonth[month]; boolean isLeapYear = new GregorianCalendar().isLeapYear(year); if (isLeapYear && month == 2) { day++; } GregorianCalendar gc = new GregorianCalendar(year, month - 1, day); java.util.Date monthEndDate = new java.util.Date(gc.getTime().getTime()); return monthEndDate; } public static void main(String[] args) { int month = 3; int year = 2076; final java.util.Date calculatedDate = calculateMonthEndDate(month, year); SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd"); format.format(calculatedDate); System.out.println("Calculated month end date : " + calculatedDate); }
source share