How to get the last date of a particular month with JodaTime?

后端 未结 4 1194
礼貌的吻别
礼貌的吻别 2020-12-02 11:36

I need to get the first date (as org.joda.time.LocalDate) of a month and the last one. Getting the first is trivial, but getting the last seems to need some log

4条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-02 12:20

    Using JodaTime,we can do this :

    
        public static final Integer CURRENT_YEAR = DateTime.now().getYear();
    
        public static final Integer CURRENT_MONTH = DateTime.now().getMonthOfYear();
    
        public static final Integer LAST_DAY_OF_CURRENT_MONTH = DateTime.now()
                .dayOfMonth().getMaximumValue();
    
        public static final Integer LAST_HOUR_OF_CURRENT_DAY = DateTime.now()
                .hourOfDay().getMaximumValue();
    
        public static final Integer LAST_MINUTE_OF_CURRENT_HOUR = DateTime.now().minuteOfHour().getMaximumValue();
    
        public static final Integer LAST_SECOND_OF_CURRENT_MINUTE = DateTime.now().secondOfMinute().getMaximumValue();
    
    
        public static DateTime getLastDateOfMonth() {
            return new DateTime(CURRENT_YEAR, CURRENT_MONTH,
                    LAST_DAY_OF_CURRENT_MONTH, LAST_HOUR_OF_CURRENT_DAY,
                    LAST_MINUTE_OF_CURRENT_HOUR, LAST_SECOND_OF_CURRENT_MINUTE);
        }

    As describe here in my small gist on github : A JodaTime and java.util.Date Util Class with a lot of usefull functions.

提交回复
热议问题