Java -- Calendar时间工具类

public class DateUtils {
	/**
     * 获取每天的开始时间 2023-10-17 00:00:00.000
     * @return
     */
    public static long getDayStartTime() {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        long todayStart = calendar.getTimeInMillis();
        return todayStart;

    }

    /**
     * 获取每天的结束时间 2023-10-17 23:59:59.999
     * @return
     */
    public static long getDayEndTime() {
        Calendar dateEnd = Calendar.getInstance();
//        dateEnd.setTime(date);
        dateEnd.set(Calendar.HOUR_OF_DAY, 23);
        dateEnd.set(Calendar.MINUTE, 59);
        dateEnd.set(Calendar.SECOND, 59);
        long todayEnd = dateEnd.getTimeInMillis();
        return todayEnd;
    }

    /**
     * 获取每小时的开始时间 10:00:00.000
     * @return
     */
    public static long getHourStartTime() {
        Calendar calendar = Calendar.getInstance();

        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 000);
        long hourStart = calendar.getTimeInMillis();
        return hourStart;

    }

    /**
     * 获取每小时的结束时间 10:59:59.999
     *
     * @return
     */
    public static long getHourEndTime() {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.MINUTE, 59);
        calendar.set(Calendar.SECOND, 59);
        calendar.set(Calendar.MILLISECOND, 999);
        long hourEnd= calendar.getTimeInMillis();
        return hourEnd;
    }
}

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