How to get start and end date of a year?

前端 未结 15 1772
清酒与你
清酒与你 2021-02-05 02:57

I have to use the Java Date class for this problem (it interfaces with something out of my control).

How do I get the start and end date of a year and then

15条回答
  •  深忆病人
    2021-02-05 03:22

        // suppose that I have the following variable as input
        int year=2011;
        Calendar calendarStart=Calendar.getInstance();
        calendarStart.set(Calendar.YEAR,year);
        calendarStart.set(Calendar.MONTH,0);
        calendarStart.set(Calendar.DAY_OF_MONTH,1);
        // returning the first date
        Date startDate=calendarStart.getTime();
    
        Calendar calendarEnd=Calendar.getInstance();
        calendarEnd.set(Calendar.YEAR,year);
        calendarEnd.set(Calendar.MONTH,11);
        calendarEnd.set(Calendar.DAY_OF_MONTH,31);
    
        // returning the last date
        Date endDate=calendarEnd.getTime();
    

    To iterate, you should use the calendar object and increment the day_of_month variable

    Hope that it can help

提交回复
热议问题