日期类的一些处理小结2

public class DateUtil {
    private static final SimpleDateFormat FORMAT = new SimpleDateFormat(
            "yy/MM/dd HH:mm");
    private static final SimpleDateFormat formatStringScd = new SimpleDateFormat(
            "yyyy-MM-dd HH:mm:ss");

    public static int compare(Date d1, Date d2) {
        String str1 = FORMAT.format(d1);
        String str2 = FORMAT.format(d2);
        int result = str1.compareTo(str2);
        if (result > 0) {
            return 1;
        } else if (result == 0) {
            return 0;
        } else {
            return -1;
        }
    }

    public static Boolean betweenTowDate(Date startDate, Date endDate, Date inDate) {
        //只取数据库的小时和分钟作为每天执行的时间段
        startDate.setYear(inDate.getYear());
        startDate.setMonth(inDate.getMonth());
        startDate.setDate(inDate.getDate());
        endDate.setYear(inDate.getYear());
        endDate.setMonth(inDate.getMonth());
        endDate.setDate(inDate.getDate());
        int sflag = compare(inDate, startDate);
        int eflag = compare(inDate, endDate);
        if (sflag > 0 && eflag < 0) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 时间相减 *
     */
    public static Date plusDate(Date startDate, Integer minutes) {
        startDate.setMinutes(startDate.getMinutes() - minutes);
        return startDate;
    }

    /**
     * 日期格式化成String
     * @param date
     * @return
     */
    public static String getDateScd(Date date) {
        return formatStringScd.format(date);
    }

你可能感兴趣的:(日期)