I am taking start date and end date. I am iterating from start date to end date based on working days once I cross 30th day I am breaking that condition and I want to print the exact 30th day. While I am printing the 31st date it's not giving valid format.it's giving below output:
31st date isjava.util.GregorianCalendar[time=1103308200000,areFieldsSet=true,areAllFieldsSet=true,lenient=true,zone=sun.util.calendar.ZoneInfo[id="Asia/Calcutta",offset=19800000,dstSavings=0,useDaylight=false,transitions=6,lastRule=null],firstDayOfWeek=1,minimalDaysInFirstWeek=1,ERA=1,YEAR=2004,MONTH=11,WEEK_OF_YEAR=51,WEEK_OF_MONTH=3,DAY_OF_MONTH=18,DAY_OF_YEAR=353,DAY_OF_WEEK=7,DAY_OF_WEEK_IN_MONTH=3,AM_PM=0,HOUR=0,HOUR_OF_DAY=0,MINUTE=0,SECOND=0,MILLISECOND=0,ZONE_OFFSET=19800000,DST_OFFSET=0] 31
below is my code:
import java.text.SimpleDateFormat;
import java.util.Calendar;
import org.openqa.selenium.WebDriver;
public class DateDifferences_Validation {
static WebDriver driver;
public static void main(String[] args) {
String startdate = "06/09/2004";
String enddate = "18/12/2004";
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
try
{
Calendar start = Calendar.getInstance();
start.setTime(sdf.parse(startdate));
Calendar end = Calendar.getInstance();
end.setTime(sdf.parse(enddate));
int workingDays = 0;
while(!start.after(end))
{
int day = start.get(Calendar.DAY_OF_WEEK);
if ((day != Calendar.SATURDAY) && (day != Calendar.SUNDAY))
workingDays++;
start.add(Calendar.DATE, 1);
if(workingDays>30){
System.out.println("31st date is"+ end);
break;
}
}
System.out.println(workingDays);
}
catch(Exception e)
{
e.printStackTrace();
}
}}