Start and end date of a current month

后端 未结 12 2086
我寻月下人不归
我寻月下人不归 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 03:47

    Try this Code

    Calendar calendar = Calendar.getInstance();
    int yearpart = 2010;
    int monthPart = 11;
    int dateDay = 1;
    calendar.set(yearpart, monthPart, dateDay);
    int numOfDaysInMonth = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
    System.out.println("Number of Days: " + numOfDaysInMonth);
    System.out.println("First Day of month: " + calendar.getTime());
    calendar.add(Calendar.DAY_OF_MONTH, numOfDaysInMonth-1);
    System.out.println("Last Day of month: " + calendar.getTime());
    

    Hope it helps.

    0 讨论(0)
  • 2020-11-29 03:56

    If you have the option, you'd better avoid the horrid Java Date API, and use instead Jodatime. Here is an example:

    LocalDate monthBegin = new LocalDate().withDayOfMonth(1);
    LocalDate monthEnd = new LocalDate().plusMonths(1).withDayOfMonth(1).minusDays(1);
    
    0 讨论(0)
  • 2020-11-29 03:57

    Simple and Best, Try this One

    Calendar calendar = Calendar.getInstance();
                calendar.add(Calendar.MONTH, 0);
                calendar.set(Calendar.DATE, calendar.getActualMinimum(Calendar.DAY_OF_MONTH));
                Date monthFirstDay = calendar.getTime();
                calendar.set(Calendar.DATE, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
                Date monthLastDay = calendar.getTime();
    
            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
            String startDateStr = df.format(monthFirstDay);
            String endDateStr = df.format(monthLastDay);
    
            Log.e("DateFirstLast",startDateStr+" "+endDateStr);
    
    0 讨论(0)
  • 2020-11-29 04:01

    There you go:

    public Pair<Date, Date> getDateRange() {
        Date begining, end;
    
        {
            Calendar calendar = getCalendarForNow();
            calendar.set(Calendar.DAY_OF_MONTH,
                    calendar.getActualMinimum(Calendar.DAY_OF_MONTH));
            setTimeToBeginningOfDay(calendar);
            begining = calendar.getTime();
        }
    
        {
            Calendar calendar = getCalendarForNow();
            calendar.set(Calendar.DAY_OF_MONTH,
                    calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
            setTimeToEndofDay(calendar);
            end = calendar.getTime();
        }
    
        return Pair.of(begining, end);
    }
    
    private static Calendar getCalendarForNow() {
        Calendar calendar = GregorianCalendar.getInstance();
        calendar.setTime(new Date());
        return calendar;
    }
    
    private static void setTimeToBeginningOfDay(Calendar calendar) {
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
    }
    
    private static void setTimeToEndofDay(Calendar calendar) {
        calendar.set(Calendar.HOUR_OF_DAY, 23);
        calendar.set(Calendar.MINUTE, 59);
        calendar.set(Calendar.SECOND, 59);
        calendar.set(Calendar.MILLISECOND, 999);
    }
    

    PS: Pair class is simply a pair of two values.

    0 讨论(0)
  • 2020-11-29 04:03

    With the date4j library :

    dt.getStartOfMonth();
    dt.getEndOfMonth();
    
    0 讨论(0)
  • 2020-11-29 04:05

    Making it more modular, you can have one main function that calculates startDate or EndDate and than you can have individual methods to getMonthStartDate, getMonthEndDate and to getMonthStartEndDate. Use methods as per your requirement.

    public static String getMonthStartEndDate(){
        String start = getMonthDate("START");
        String end = getMonthDate("END");
        String result = start + " to " + end;
        return result;
    }
    
    public static String getMonthStartDate(){
        String start = getMonthDate("START");
        return start;
    }
    
    public static String getMonthEndDate(){
        String end = getMonthDate("END");
        return end;
    }
    
    /**
     * @param filter 
     * START for start date of month e.g.  Nov 01, 2013
     * END for end date of month e.g.  Nov 30, 2013
     * @return
     */
    public static String getMonthDate(String filter){
                String MMM_DD_COMMA_YYYY       = "MMM dd, yyyy";
        SimpleDateFormat sdf = new SimpleDateFormat(MMM_DD_COMMA_YYYY);
        sdf.setTimeZone(TimeZone.getTimeZone("PST"));
        sdf.format(GregorianCalendar.getInstance().getTime());
    
        Calendar cal =  GregorianCalendar.getInstance();
        int date = cal.getActualMinimum(Calendar.DATE);
        if("END".equalsIgnoreCase(filter)){
            date = cal.getActualMaximum(Calendar.DATE);
        }
        cal.set(Calendar.DATE, date);
        String result =  sdf.format(cal.getTime());
        System.out.println(" " + result  );
    
        return result;
    }
    
    0 讨论(0)
提交回复
热议问题