How to get start and end date of a year?

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

    First and Last day of Year

    import java.util.Calendar
    import java.text.SimpleDateFormat
    
    val parsedDateInt = new SimpleDateFormat("yyyy-MM-dd")
    
    val cal2 = Calendar.getInstance()
    cal2.add(Calendar.MONTH, -(cal2.get(Calendar.MONTH)))
    cal2.set(Calendar.DATE, 1)
    val firstDayOfYear = parsedDateInt.format(cal2.getTime)
    
    
    cal2.add(Calendar.MONTH, (11-(cal2.get(Calendar.MONTH))))
    cal2.set(Calendar.DATE, cal2.getActualMaximum(Calendar.DAY_OF_MONTH))
    val lastDayOfYear = parsedDateInt.format(cal2.getTime)
    

提交回复
热议问题