获取日期的最早和最晚时间

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.time.DateUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

@Slf4j
public class DateUtil {
    private DateUtil() {
    }

 
 /**
     * 将日期时间归至零点
     *
     * @param date
     * @return
     */
    public static Date dateToBeginOfDay(Date date) {
        if (date != null) {
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(date);
            calendar.set(Calendar.HOUR_OF_DAY, 0);
            calendar.set(Calendar.MINUTE, 0);
            calendar.set(Calendar.SECOND, 0);
            calendar.set(Calendar.MILLISECOND, 0);
            return calendar.getTime();
        }
        return null;
    }

  /**
     * 将日期时间转化成一天中的最大值
     *
     * @param endDate 结束时间
     * @return
     */
    public static Date convertToBiggestTime(Date endDate) {
        if (null == endDate) {
            return endDate;
        }
        Date newDate;
        Calendar calendar = DateUtils.toCalendar(endDate);
        calendar.set(Calendar.HOUR_OF_DAY,
                calendar.getActualMaximum(Calendar.HOUR_OF_DAY));
        calendar.set(Calendar.MINUTE,
                calendar.getActualMaximum(Calendar.MINUTE));
        calendar.set(Calendar.SECOND,
                calendar.getActualMaximum(Calendar.SECOND));
        newDate = calendar.getTime();
        return newDate;
    }
}
posted @ 2021-01-14 15:24  vwa  阅读(1281)  评论(0编辑  收藏  举报