How to get start and end date of a year?

前端 未结 15 1773
清酒与你
清酒与你 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:31

    An improvement over Srini's answer.
    Determine the last date of the year using Calendar.getActualMaximum.

    Calendar cal = Calendar.getInstance();
    
    calDate.set(Calendar.DAY_OF_YEAR, 1);
    Date yearStartDate = calDate.getTime();
    
    calDate.set(Calendar.DAY_OF_YEAR, calDate.getActualMaximum(Calendar.DAY_OF_YEAR));
    Date yearEndDate = calDate.getTime();
    

提交回复
热议问题