Start and end date of a current month

后端 未结 12 2087
我寻月下人不归
我寻月下人不归 2020-11-29 03:08

I need the start date and the end date of the current month in Java. When the JSP page is loaded with the current month it should automatically calculate the start and end d

相关标签:
12条回答
  • 2020-11-29 04:06
    Date begining, ending;
    Calendar calendar_start =BusinessUnitUtility.getCalendarForNow();
      calendar_start.set(Calendar.DAY_OF_MONTH,calendar_start.getActualMinimum(Calendar.DAY_OF_MONTH));
      begining = calendar_start.getTime();
      String start= DateDifference.dateToString(begining,"dd-MMM-yyyy");//sdf.format(begining);
    
    
       //            for End Date of month
      Calendar calendar_end = BusinessUnitUtility.getCalendarForNow();
      calendar_end.set(Calendar.DAY_OF_MONTH,calendar_end.getActualMaximum(Calendar.DAY_OF_MONTH));
          ending = calendar_end.getTime();
          String end=DateDifference.dateToString(ending,"dd-MMM-yyyy");//or sdf.format(end);
    
    enter code here
    
    
    
    public static Calendar getCalendarForNow() {
            Calendar calendar = GregorianCalendar.getInstance();
            calendar.setTime(new Date());
            return calendar;
        }
    
    0 讨论(0)
  • 2020-11-29 04:07

    Try LocalDate from Java 8:

    LocalDate today = LocalDate.now();
    System.out.println("First day: " + today.withDayOfMonth(1));
    System.out.println("Last day: " + today.withDayOfMonth(today.lengthOfMonth()));
    
    0 讨论(0)
  • 2020-11-29 04:08

    For Java 8+, below method will given current month first & last dates as LocalDate instances.

    public static LocalDate getCurrentMonthFirstDate() {
        return LocalDate.ofEpochDay(System.currentTimeMillis() / (24 * 60 * 60 * 1000) ).withDayOfMonth(1);
    }
    
    public static LocalDate getCurrentMonthLastDate() {
        return LocalDate.ofEpochDay(System.currentTimeMillis() / (24 * 60 * 60 * 1000) ).plusMonths(1).withDayOfMonth(1).minusDays(1);
    }
    

    Side note: Using LocalDate.ofEpochDay(...) instead of LocalDate.now() gives much improved performance. Also, using the millis-in-a-day expression instead of the end value, which is 86400000 is performing better. I initially thought the latter would perform better than the the expression :P

    0 讨论(0)
  • 2020-11-29 04:10
    Calendar c = Calendar.getInstance();
        int year = c.get(Calendar.YEAR);
        int month = c.get(Calendar.MONTH);
        int day = 1;
        c.set(year, month, day);
        int numOfDaysInMonth = c.getActualMaximum(Calendar.DAY_OF_MONTH);
        System.out.println("First Day of month: " + c.getTime());
        c.add(Calendar.DAY_OF_MONTH, numOfDaysInMonth-1);
        System.out.println("Last Day of month: " + c.getTime());
    
    0 讨论(0)
  • 2020-11-29 04:10
    public static void main(String[] args) 
        {
            LocalDate today = LocalDate.now();
            System.out.println("First day: " + 
    today.withDayOfMonth(1));
            System.out.println("Last day: " + today.withDayOfMonth(today.lengthOfMonth()))
        }
    
    0 讨论(0)
  • 2020-11-29 04:10

    You can implement it as below:

    public void FirstAndLastDate() {
    
        SimpleDateFormat sdf = new SimpleDateFormat("yyy-MM-dd");
        //start date of month
        calendarStart = Calendar.getInstance();
        calendarStart.set(Integer.parseInt((new SimpleDateFormat("yyyy")).format(new Date().getTime()))
                , Integer.parseInt((new SimpleDateFormat("MM")).format(new Date().getTime()))
                , Calendar.getInstance().getActualMinimum(Calendar.DAY_OF_MONTH));
    
        //End Date of month
        calendarEnd = Calendar.getInstance();
        calendarEnd.set(Integer.parseInt((new SimpleDateFormat("yyyy")).format(new Date().getTime()))
                , Integer.parseInt((new SimpleDateFormat("MM")).format(new Date().getTime()))
                , Calendar.getInstance().getActualMaximum(Calendar.DAY_OF_MONTH));
    
        Toast.makeText(this, sdf.format(calendarStart.getTime()) + "\n" + sdf.format(calendarEnd.getTime()), Toast.LENGTH_SHORT).show();
    
    }
    
    0 讨论(0)
提交回复
热议问题