Get first date of current month in java

前端 未结 9 1790
日久生厌
日久生厌 2020-11-29 05:28

I am trying to get to and from date where ToDate will have previous date and FromDate will have first date of the current month. For January it wou

9条回答
  •  半阙折子戏
    2020-11-29 06:27

    try

        Calendar c = Calendar.getInstance();   // this takes current date
        c.set(Calendar.DAY_OF_MONTH, 1);
        System.out.println(c.getTime());       // this returns java.util.Date
    

    Updated (Since Java 8):

    import java.time.LocalDate;
    LocalDate todaydate = LocalDate.now();
    System.out.println("Months first date in yyyy-mm-dd: " +todaydate.withDayOfMonth(1));
    

提交回复
热议问题