0
public static String CalculateNoofDays(String txtDateValue ) throws Exception   
     {
        String Days="";
        String Count="";
        String ar[] = new String[2];
        ar[0]= Days;
        ar[1] =Count;



        String MonthOfName = "Unknown";     
        String days=""; 
        int number_Of_DaysInMonth = 0;

        String[] DateValues  = txtDateValue.split("/");
        //Dateformat  df2 = new SimpleDateFormat("HH:mm:ss a MMM dd, yyyy"); 
        //Date cdate = df2.parse(txtDateValue); 
        String month = DateValues[0];
        int mon =Integer.parseInt(month);
        //mon = mon-1;
        month = String.valueOf(mon);
        String day = DateValues[1];
        String year = DateValues[2];
        int result = Integer.parseInt(year);            


        int monthcount = mon;
        int yearname = result;

        switch (monthcount) {
        case 1:
            MonthOfName = "January";
            number_Of_DaysInMonth = 31;

            break;

        case 2:
            MonthOfName = "February";
            if ((yearname % 400 == 0) || ((yearname % 4 == 0) && (yearname % 100 != 0))) {
                number_Of_DaysInMonth = 29;
            } else {
                number_Of_DaysInMonth = 28;
            }
            break;
        case 3:
            MonthOfName = "March";
            number_Of_DaysInMonth = 31;
            break;
        case 4:
            MonthOfName = "April";
            number_Of_DaysInMonth = 30;
            break;
        case 5:
            MonthOfName = "May";
            number_Of_DaysInMonth = 31;
            break;
        case 6:
            MonthOfName = "June";
            number_Of_DaysInMonth = 30;
            break;
        case 7:
            MonthOfName = "July";
            number_Of_DaysInMonth = 31;
            break;
        case 8:
            MonthOfName = "August";
            number_Of_DaysInMonth = 31;
            break;
        case 9:
            MonthOfName = "September";
            number_Of_DaysInMonth = 30;
            break;
        case 10:
            MonthOfName = "October";
            number_Of_DaysInMonth = 31;
            break;
        case 11:
            MonthOfName = "November";
            number_Of_DaysInMonth = 30;
            break;
        case 12:
            MonthOfName = "December";
            number_Of_DaysInMonth = 31;

    }  

    int Totaldays=number_Of_DaysInMonth;
     Count=String.valueOf(Totaldays);
    int CurrentDays = Integer.parseInt(day);
    int tal=Totaldays-CurrentDays;

    days= String.valueOf(tal);
    days=Days;

    return Days;
    //return ar;
}

Note: Here What I am trying to do is Based On ExcelDateInput Iam Trying To get Month and Calculate no of days in that month and Subtract the Remaining days here I want No of Days in that Month And Remaining Days
Is this best Approach or any other Easy Way

1 Answer 1

2

This is the code that would work in most of Java versions:

public static void main(String[] args) throws Exception {
    String test = "2020/02/20";
    CalculateNoofDays(test);
}

public static String CalculateNoofDays(String txtDateValue ) throws Exception
{
    Date dateDateValue = new SimpleDateFormat("yyyy/MM/dd").parse(txtDateValue);
    Calendar calendar = GregorianCalendar.getInstance();
    calendar.setTime(dateDateValue);
    int daysMax = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
    System.out.println("Number of days in a month: " + daysMax);
    int currentDay = calendar.get(Calendar.DAY_OF_MONTH);
    System.out.println("Current day's number: " + currentDay);
    int result = daysMax - currentDay;
    System.out.println("Result: " + result);
    return String.valueOf(result);
}

Couple of notes here:

  1. I preserved your method's signature. However it is better to return numeric types for the methods which count something
  2. You will have to know the date format that your date string would represent
  3. Remove logs. They are just for you to play around with values
  4. Remove unnecessary exceptions from signature.

So the nice-looking method would look like this one:

public static int calculateNoOfDays(String txtDateValue) throws ParseException {
    Calendar calendar = GregorianCalendar.getInstance();
    calendar.setTime(new SimpleDateFormat("yyyy/MM/dd").parse(txtDateValue));
    return calendar.getActualMaximum(Calendar.DAY_OF_MONTH) - calendar.get(Calendar.DAY_OF_MONTH);
}

Then if you need to incorporate complex data type that would contain both the values in one object then you can come up with arrays or deefine a dedicated class like this:

class CalcResult{

    final int numberOfDaysInMonth;
    final int remainingDays;

    public CalcResult(int numberOfDaysInMonth, int remainingDays){
        this.numberOfDaysInMonth = numberOfDaysInMonth;
        this.remainingDays = remainingDays;
    }

    public int getNumberOfDaysInMonth() {
        return numberOfDaysInMonth;
    }

    public int getRemainingDays() {
        return remainingDays;
    }

}

Then rework your method to look like this:

public static CalcResult CalculateNoofDays(String txtDateValue) throws ParseException {
    Calendar calendar = GregorianCalendar.getInstance();
    calendar.setTime(new SimpleDateFormat("yyyy/MM/dd").parse(txtDateValue));
    return new CalcResult(
            calendar.getActualMaximum(Calendar.DAY_OF_MONTH),
            calendar.getActualMaximum(Calendar.DAY_OF_MONTH) - calendar.get(Calendar.DAY_OF_MONTH));
}

And finally test like this:

public static void main(String[] args) throws Exception {
    String test = "2020/02/20";
    CalcResult res = CalculateNoofDays(test);
    System.out.println("Number Of Days In Month: " + res.getNumberOfDaysInMonth());
    System.out.println("Number Of Remaining Days: " + res.getRemainingDays());
}
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.