码迷,mamicode.com
首页 > 编程语言 > 详细

日期时间格式化(java)

时间:2016-10-24 21:04:25      阅读:205      评论:0      收藏:0      [点我收藏+]

标签:cond   cat   时间类   one   import   com   .class   package   date   

package com.lvxh.service;

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

import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;

/**
 * . 日期工具 2015-09-10
 *
 * @since V100R001C02
 * @version V100R001C02
 */
public abstract class DateUtil {

    /**
     * 初始化日志类
     */
    private static final Logger LOG = Logger.getLogger(DateUtil.class);

    /**
     * .日期格式
     */
    private static final String DATE_FORMAT = "yyyy-MM-dd";

    /**
     * .日期格式
     */
    public static final String DATETIME_FORMAT = "yyyy-MM-dd HH:mm:ss";

    /**
     * .日期格式
     */
    private static final int MILLISENCOND_OF_MINUTE = 60 * 1000;

    /**
     * . 日期拷贝
     *
     * @param date
     *            源日期对象
     * @return 拷贝后的日期对象
     */
    public static Date clone(Date date) {
        if (date != null) {
            return (Date) date.clone();
        }
        return null;
    }

    /**
     * 时间类型转字符串
     *
     * @return String
     */
    public static String getToday() {

        return dateToStr(new Date());
    }

    /**
     * 时间类型转字符串
     *
     * @param date
     *            Date
     *
     * @return String
     */
    public static String dateToStr(Date date) {
        return dateToStr(date, DATE_FORMAT);
    }
    
    /**
     * 时间类型转字符串
     *
     * @param date
     *            Date
     *
     * @param format
     *            格式
     *
     * @return String
     */
    public static String dateToStr(Date date, String format) {

        if (date == null) {
            return "";
        } else {
            SimpleDateFormat sdf = new SimpleDateFormat(format);
            return sdf.format(date);
        }
    }

    /**
     * 时间字符串转换为日期
     *
     * @param date
     *            String 需要转换的字符串
     * @param format
     *            转换格式
     * @return Date 返回日期
     */
    public static Date strToDate(String date, String format) {
        Date time = null;
        if (StringUtils.isEmpty(date)) {
            LOG.info("date is null ");
        } else {
            SimpleDateFormat sdf = new SimpleDateFormat(format);
            try {
                time = sdf.parse(date);
            } catch (ParseException e) {
                LOG.error("parse time exception ", e);
            }
        }
        return time;
    }

    /**
     * 功能描述:时间相减得到分钟数
     *
     * @param beginDate
     *            beginDate
     * @param endDate
     *            endDate
     * @return long
     */
    public static long subMinutes(Date beginDate, Date endDate) {
        return (endDate.getTime() - beginDate.getTime()) / MILLISENCOND_OF_MINUTE;
    }
}

日期时间格式化(java)

标签:cond   cat   时间类   one   import   com   .class   package   date   

原文地址:http://www.cnblogs.com/lvxh/p/5994214.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!