com.baomidou.framework.common.util.DateUtil.java Source code

Java tutorial

Introduction

Here is the source code for com.baomidou.framework.common.util.DateUtil.java

Source

/**
 * Copyright (c) 2011-2014, liuchangng@qq.com.
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package com.baomidou.framework.common.util;

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

import org.apache.commons.lang.time.DateUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * <p>
 * ?
 * </p>
 * 
 * @author hubin
 * @Date 2016-04-16
 */
public class DateUtil extends DateUtils {
    private static Logger logger = LoggerFactory.getLogger(DateUtil.class);
    /**  */
    public final static long MS = 1;
    /** ? */
    public final static long SECOND_MS = MS * 1000;
    /** ? */
    public final static long MINUTE_MS = SECOND_MS * 60;
    /** ?? */
    public final static long HOUR_MS = MINUTE_MS * 60;
    /** ? */
    public final static long DAY_MS = HOUR_MS * 24;

    /** ??? */
    private final static SimpleDateFormat NORM_DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd");
    /** ? */
    private final static SimpleDateFormat NORM_DATETIME_FORMAT = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    /** HTTP? */
    private final static SimpleDateFormat HTTP_DATETIME_FORMAT = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss z",
            Locale.US);

    /**
     * 
     * ?? yyyy-MM-dd HH:mm:ss
     * 
     * @return ??
     * 
     */
    public static String now() {
        return formatDateTime(new Date());
    }

    /**
     * 
     * ?? yyyy-MM-dd
     * 
     * @return ??
     * 
     */
    public static String today() {
        return formatDate(new Date());
    }

    // ------------------------------------ Format start
    // ----------------------------------------------

    /**
     * 
     * ???
     * 
     * @param date
     *            ?
     * 
     * @param format
     *            ?
     * 
     * @return ??
     * 
     */
    public static String format(Date date, String format) {
        return new SimpleDateFormat(format).format(date);
    }

    /**
     * 
     * ? yyyy-MM-dd HH:mm:ss
     * 
     * @param date
     *            ?
     * 
     * @return ??
     * 
     */
    public static String formatDateTime(Date date) {
        // return format(d, "yyyy-MM-dd HH:mm:ss");

        return NORM_DATETIME_FORMAT.format(date);
    }

    /**
     * 
     * ?Http?
     * 
     * @param date
     *            ?
     * 
     * @return HTTP?
     * 
     */
    public static String formatHttpDate(Date date) {
        // return new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss z",
        // Locale.US).format(date);

        return HTTP_DATETIME_FORMAT.format(date);
    }

    /**
     * 
     * ? yyyy-MM-dd
     * 
     * @param date
     *            ?
     * 
     * @return ??
     * 
     */
    public static String formatDate(Date date) {
        // return format(d, "yyyy-MM-dd");

        return NORM_DATE_FORMAT.format(date);
    }
    // ------------------------------------ Format end
    // ----------------------------------------------

    // ------------------------------------ Parse start
    // ----------------------------------------------

    /**
     * 
     * ??Date
     * 
     * @param dateString
     *            ?
     * 
     * @param format
     *            ?yyyy-MM-dd
     * 
     * @return 
     * 
     */
    public static Date parse(String dateString, String format) {
        try {
            return (new SimpleDateFormat(format)).parse(dateString);
        } catch (ParseException e) {
            logger.error("Parse " + dateString + " with format " + format + " error!", e);
        }
        return null;
    }

    /**
     * 
     * ?yyyy-MM-dd HH:mm:ss
     * 
     * @param dateString
     *            ?
     * 
     * @return 
     * 
     */
    public static Date parseDateTime(String dateString) {
        // return parse(s, "yyyy-MM-dd HH:mm:ss");

        try {
            return NORM_DATETIME_FORMAT.parse(dateString);
        } catch (ParseException e) {
            logger.error("Parse " + dateString + " with format " + NORM_DATETIME_FORMAT.toPattern() + " error!", e);
        }
        return null;
    }

    /**
     * 
     * ?yyyy-MM-dd
     * 
     * @param dateString
     *            ?
     * 
     * @return 
     * 
     */
    public static Date parseDate(String dateString) {
        // return parse(s, "yyyy-MM-dd");

        try {
            return NORM_DATE_FORMAT.parse(dateString);
        } catch (ParseException e) {
            logger.error("Parse " + dateString + " with format " + NORM_DATE_FORMAT.toPattern() + " error!", e);
        }
        return null;
    }
    // ------------------------------------ Parse end
    // ----------------------------------------------

    /**
     * 
     * ????
     * 
     * @param date
     *            
     * 
     * @param calendarField
     *            ??????Calendar
     * 
     * @param offsite
     *            ???????????
     * 
     * @return ???
     * 
     */
    public static Date getOffsiteDate(Date date, int calendarField, int offsite) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(calendarField, offsite);
        return cal.getTime();
    }

    /**
     * 
     * <br/>
     * 
     *  minuend - subtrahend 
     * 
     * @param subtrahend
     *            ?
     * 
     * @param minuend
     *            ?
     * 
     * @param diffField
     *            ??
     * 
     * @return 
     * 
     */
    public static long dateDiff(Date subtrahend, Date minuend, long diffField) {
        long diff = minuend.getTime() - subtrahend.getTime();
        return diff / diffField;
    }
}