日期辅助类

package util;

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

/**
 * @Title: DateUtil.java
 * @Description: 日期辅助类
 */
public class DateUtil {

    /**
     * @Title: getDateString
     * @Description: 使用"yyyy-MM-dd HH:mm:ss"格式化日期
     * @param date
     * @return String 返回类型
     */
    public static String getDateString(Date date) {
        return getDateString(date, "yyyy-MM-dd HH:mm:ss");
    }

    /**
     * 
     * @param date
     * @param format
     * @return
     */
    public static String getCurDateString(Date date){
        return getDateString(date, "yyyy-MM-dd");
    }

    /**
     * @Title: getDateString
     * @Description: 使用"yyyy-MM-dd HH:mm:ss"格式化日期
     * @param long time
     * @return String 返回类型
     */
    public static String getDateString(long time) {
        Date date = new Date();
        date.setTime(time);
        return getDateString(date, "yyyy-MM-dd HH:mm:ss");
    }

    /**
     * @Title: getDateString
     * @Description: 格式化日期
     * @param date
     *            日期
     * @param format
     *            模式
     * @return String 返回类型
     */
    public static String getDateString(Date date, String format) {
        if (date != null) {
            SimpleDateFormat formatter = new SimpleDateFormat(format);
            String dateString = formatter.format(date);
            return dateString;
        }
        return null;
    }

    /**
     * @Title: parseDate
     * @Description: 解析Date,string to java.util.Date
     * @param strDate
     * @param format
     * @return Date 返回类型
     */
    public static Date parseDate(String strDate, String format) {
        if (strDate != null) {
            SimpleDateFormat formatter = new SimpleDateFormat(format);
            Date date = null;
            try {
                date = formatter.parse(strDate);
            } catch (ParseException e) {
                e.printStackTrace();
            }
            return date;
        }
        return null;
    }

    /**
     * 字符串时间转为long的时间
     * @param strDate
     * @param format
     * @return
     */
    public static int parseDateToLong(String strDate, String format) {
        if (strDate != null) {
            SimpleDateFormat formatter = new SimpleDateFormat(format);
            Date date = null;
            try {
                date = formatter.parse(strDate);
            } catch (ParseException e) {
                e.printStackTrace();
            }
            return (int)(date.getTime()/1000);
        }
        return (int) (new Date().getTime()/1000);
    }


    /***
     * 
     * @Title: getDaysBetween
     * @Description: 计算两个日期之间的天数(date1<date2)
     * @author wujl
     * @param @param date1
     * @param @param date2
     * @param @return 设定文件
     * @return int 返回类型
     */
    public static int getDaysBetween(Date date1, Date date2) {
        long from = date1.getTime();
        long to = date2.getTime();
        return (int) (to - from) / (1000 * 60 * 60 * 24);
    }

    public static int getHour(Date time) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(time);
        int hourTime = calendar.get(Calendar.HOUR_OF_DAY);
        return hourTime;
    }

    public static int getDayOfMonth(Date time) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(time);
        int hourTime = calendar.get(Calendar.DAY_OF_MONTH);
        return hourTime;
    }

    public static int getDayOfYear(Date time) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(time);
        int hourTime = calendar.get(Calendar.DAY_OF_YEAR);
        return hourTime;
    }

    public static int getMonth(Date time) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(time);
        int hourTime = calendar.get(Calendar.MONTH);
        return hourTime;
    }

    public static int getMinute(Date time) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(time);
        int hourTime = calendar.get(Calendar.MINUTE);
        return hourTime;
    }

    public static int getYear(Date time) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(time);
        int hourTime = calendar.get(Calendar.YEAR);
        return hourTime;
    }

    public static int getNowTimeStamp() {
        return (int) (System.currentTimeMillis() / 1000);
    }

    /**
     * 在date上面加减天数
     * @param date
     * @param num
     * @return
     */
    public static Date addDayByDate(Date date, int num){
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.DAY_OF_YEAR, num);
        return calendar.getTime();
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

我要修改昵称

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值