日期工具,校验当年开始年份,结束年份,当月开始日期,结合素日期


import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.temporal.TemporalAdjusters;
import java.util.Calendar;
import java.util.Date;

/***
 * 日期处理
 */
public class DateUtil {


    /****
     * 校验开始日期是否为输入日期的,月份的第一天
     */
    public boolean isMonthOneDay(Date monthOneDay) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(monthOneDay);
        return calendar.get(Calendar.DAY_OF_MONTH) == 1;
    }


    /**
     * 获取输入开始日期的最后一天
     *
     * @return yyyy-mm-dd
     */
    public boolean isMonthLastDay(Date monthLastDay) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(monthLastDay);
        return calendar.get(Calendar.DAY_OF_MONTH) == calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
    }

    /***
     * 每年的第一天
     * @param yearOneDay
     */
    public boolean isYearOneDay(Date yearOneDay) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(yearOneDay);

        int dayOfYear = calendar.get(Calendar.DAY_OF_YEAR);
        if (dayOfYear == 1) {
            return true;
        }
        return false;
    }

    /****
     * 每年的最后一天
     */
    public boolean isYearLastDay(Date yearLastDay) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String newYearLastDay = sdf.format(yearLastDay);

        //规定的每年最后一天日期为
        String substring = newYearLastDay.substring(0, 4);
        String gdLastYearDay = substring + "-" + "12-"+"31";

        if(newYearLastDay == gdLastYearDay){
            return true;
        }

        return false;
    }

    /***
     *开始日期、结束日期是否是同一个月
     * @param startDay
     * @param endDay
     * @return
     */
    public boolean isInMonth(Date startDay, Date endDay) {
        Calendar calendar1 = Calendar.getInstance();
        calendar1.setTime(startDay);
        Calendar calendar2 = Calendar.getInstance();
        calendar2.setTime(endDay);
       /* int year1 = calendar1.get(Calendar.YEAR);
        int year2 = calendar2.get(Calendar.YEAR);
        int month1 = calendar1.get(Calendar.MONTH);
        int month2 = calendar2.get(Calendar.MONTH);*/
        return calendar1.get(Calendar.YEAR) == calendar2.get(Calendar.YEAR) && calendar1.get(Calendar.MONTH) == calendar2.get(Calendar.MONTH);
    }

    /***
     * 开始日期、结束日期是否为同一年
     * @param startDay
     * @param endDay
     * @return
     */
    public boolean isInYear(Date startDay, Date endDay) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

        Calendar startCalendar = Calendar.getInstance();
        startCalendar.setTime(startDay);

        Calendar endCalendar = Calendar.getInstance();
        endCalendar.setTime(endDay);

        int startYearInt = startCalendar.get(Calendar.YEAR);
        int endYearInt = endCalendar.get(Calendar.YEAR);

        String startYear = sdf.format(startDay);
        String endYear =sdf.format(endDay);

        int startMonthInt = Integer.valueOf(startYear.substring(5,7));
        int endMonthInt = Integer.valueOf(endYear.substring(5,7));

        int startDayInt =  Integer.valueOf(startYear.substring(8,10));
        int endDayInt =  Integer.valueOf(endYear.substring(8,10));



        if (startYearInt == endYearInt /*&& startMonthInt==1 && endMonthInt==12 && startDayInt==1 && endDayInt == 31*/) {
            return true;
        }
        return false;
    }
}

你可能感兴趣的:(java,java)