Date帮助类

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * 时间工具类;
 * 
 */
public class DateUtil
{
    private static Log log = LogFactory.getLog(DateUtil.class);

    private static String dateJavaFormat;

    private static SimpleDateFormat format;

    static
    {
        dateJavaFormat = "dd/MM/yyyy HH:mm:ss";
        format = new SimpleDateFormat(dateJavaFormat);
    }

    /**
     * 获取数据库中的格式日期;
     * @return
     */
    public static String getDatabaseDateFormat()
    {
        String dbFormat = "dd/MM/yyyy HH:mm:ss";
        return dbFormat;
    }

    /**
     * 获取当前日期;
     * @return
     */
    public static Date getCurrentDate()
    {
        log.debug("start DateUtil getCurrentDate Method.");
        Date currDate = DateUtil.stringConvertDate(format.format(new Date()));
        log.debug("DateUtil getCurrentDate [current Date]:" + currDate);
        log.debug("end DateUtil getCurrentDate Method.");
        return currDate;
    }

    /**
     * 两个日期相减,获取分钟.
     * @param begin
     * @param end
     * @return
     */
    public static long countMinute(Date begin, Date end)
    {
        int minute = 0;
        long total_minute = 0;
        Date begin_date = begin;
        Date end_date = end;

        total_minute = (end_date.getTime() - begin_date.getTime())
                / (1000 * 60);
        minute = (int) total_minute % 60;
        return minute;
    }

    /**
     * 两个日期相减,获取小时.
     * @param begin
     * @param end
     * @return
     */
    public static long countHour(Date begin, Date end)
    {
        int hour = 0;
        long total_minute = 0;

        total_minute = (end.getTime() - begin.getTime()) / (1000 * 60);
        hour = (int) total_minute / 60;
        return hour;
    }

    /**
     * 日期类型转成字符串;
     * @param date
     * @return
     */
    public static String dateConvetString(Date date)
    {
        String after_date = null;
        if (date != null)
        {
            after_date = format.format(date);
        }
        return after_date;
    }

    /**
     * 字符串类型转成日期类型;
     * @param date
     * @return
     */
    public static Date stringConvertDate(String date)
    {
        log.debug("start stringConvertDate method.");
        final String MSG = "end stringConvertDate method.";

        Date after_date = null;
        if (date != null && !"".equals(date))
        {
            try
            {
                after_date = format.parse(date);
                log.debug("stringConvertDate method->[after_date]:"
                        + after_date);
            }
            catch (ParseException e)
            {
                // TODO Auto-generated catch block
                e.printStackTrace();
                log.error("String Convert Date error:", e);
            }
        }
        log.debug(MSG);
        return after_date;
    }
}

你可能感兴趣的:(Date)