常用的日期操作工具类

package com.common.utils;

import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 日期转换类
 */
public class DateUtils {

    public static final String ISO_DATE_FORMAT = "yyyy-MM-dd";
    public static final String ISO_DATE_T_FORMAT = "yyyyMM";
    public static final String ISO_DATETIME_NO_T_FORMAT = "yyyy-MM-dd HH:mm:ss";
    public static final String ISO_DATE_NO_HYPHEN_FORMAT = "yyyyMMdd";
    public static final String ISO_DATETIME_NO_HYPHEN_AND_T_FORMAT = "yyyyMMdd HH:mm:ss";
    public static final String ISO_WEEK_FORMAT = "yyyyWW";

    /**
     * 将timestamp转换为Date类型
     */
    public static Date convertTimestampToDate(Timestamp timestamp) {
        return new Date(timestamp.getTime());
    }

    /**
     * 得到当前的timestamp
     */
    public static Timestamp getCurrentTimestamp() {
        return new java.sql.Timestamp((new Date()).getTime());
    }

    /**
     * 将日期按照指定模式转换为字符串
     *
     * @param date    日期
     * @param pattern 指定模式
     * @return 转换后的字符串
     */
    public static String convertToString(final Date date, final String pattern) {
        if (date != null) {
            return new SimpleDateFormat(pattern).format(date);
        } else {
            return null;
        }
    }

    /**
     * 将字符串按照指定格式转换为日期
     *
     * @param source  字符串
     * @param pattern 指定格式
     * @return 日期
     * @throws ParseException
     */
    public static Date converToDate(final String source, final String pattern) throws ParseException {
        return new SimpleDateFormat(pattern).parse(source);
    }

    /**
     * 取得当前年份
     *
     * @return 当前年份
     */
    public static int getCurrentYear() {
        Calendar calendar = Calendar.getInstance();
        return calendar.get(Calendar.YEAR);
    }

    /**
     * 取得当前月份
     *
     * @return 当前月份
     */
    public static int getCurrentMonth() {
        Calendar calendar = Calendar.getInstance();
        return calendar.get(Calendar.MONTH) + 1;
    }

    /**
     * 取得当前天数
     *
     * @return 当前天数
     */
    public static int getCurrentTaday() {
        Calendar calendar = Calendar.getInstance();
        return calendar.get(Calendar.DATE);
    }

    /**
     * 取得当前月份的第一天对应的日期字符串
     */
    public static String getBeginDateOfMonth() {
        Calendar calendar = Calendar.getInstance();
        String year = String.valueOf(calendar.get(Calendar.YEAR));
        String month = calendar.get(Calendar.MONTH) < 9 ? "0" + String.valueOf(calendar.get(Calendar.MONTH) + 1)
                : String.valueOf(calendar.get(Calendar.MONTH) + 1);
        return year + "-" + month + "-01";
    }


    /**
     * 取得今天对应的日期字符串
     */
    public static String getCurrentDate() {
        return convertToString(new Date(), ISO_DATE_FORMAT);
    }

    public static String getLastDate() {
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.DATE, -1);
        String date = DateUtils.convertToString(calendar.getTime(), ISO_DATE_FORMAT);
        return date;
    }


    /**
     * 取得今天对应的日期字符串
     */
    public static String getCurrentDate1() {
        return convertToString(new Date(), ISO_DATE_T_FORMAT);
    }


    /**
     * 判断一个日期字符串是否合法
     *
     * @param date    日期字符串
     * @param pattern 日期模式
     * @return 合法则返回true
     */
    public static boolean isValidDate(final String date, final String pattern) {
        if (date != null) {
            try {
                converToDate(date, pattern);
                return true;
            } catch (ParseException e) {
                return false;
            }
        } else {
            return false;
        }
    }

    /**
     * 将日期转换为当日开始的日期
     */
    public static Date getBeginDateOfDay(final Date date) {
        try {
            return converToDate(convertToString(date, ISO_DATE_FORMAT) + " 00:00:00", ISO_DATETIME_NO_T_FORMAT);
        } catch (ParseException e) {
            throw new RuntimeException(e.getMessage(), e); // Will Never happen
        }
    }

    /**
     * 将输入的日期字符串转换为当日开始日期
     *
     * @throws ParseException
     */
    public static Date getBeginDateOfDay(final String date, final String pattern) throws ParseException {
        return converToDate(date + " 00:00:00", pattern);
    }

    /**
     * 将输入日期转换为当日结束日期
     */
    public static Date getEndDateOfDay(final Date date) {
        try {
            return converToDate(convertToString(date, ISO_DATE_FORMAT) + " 23:59:59", ISO_DATETIME_NO_T_FORMAT);
        } catch (ParseException e) {
            throw new RuntimeException(e.getMessage(), e); // Will Never happen
        }
    }

    /**
     * 将输入的日期字符串转换为当日结束日期
     *
     * @throws ParseException
     */
    public static Date getEndDateOfDay(final String date, final String pattern) throws ParseException {
        return converToDate(date + " 23:59:59", pattern);
    }

    /**
     * 求某年月的第一天
     * java里0到11分别是1到12月
     *
     * @param year
     * @param month
     * @return Date
     */
    public static Date getMonthFirst(int year, int month) {
        Calendar calendar = Calendar.getInstance();
        calendar.clear();
        calendar.set(Calendar.YEAR, year);
        calendar.set(Calendar.MONTH, month);
        Date currYearFirst = calendar.getTime();
        return currYearFirst;
    }

    /**
     * 求某年月的最后一天
     * java里0到11分别是1到12月
     *
     * @param year
     * @param month
     * @return Date
     */
    public static Date getMonthLast(int year, int month) {
        Calendar calendar = Calendar.getInstance();
        calendar.clear();
        calendar.set(Calendar.YEAR, year);
        calendar.set(Calendar.MONTH, month);
        calendar.roll(Calendar.DAY_OF_MONTH, -1);//向指定日历字段添加指定(有符号的)时间量,不更改更大的字段。负的时间量意味着向下滚动。
        Date currMonthLast = calendar.getTime();

        return currMonthLast;
    }

    /**
     * 某年某月第几周第几天 是几月几号
     *
     * @param year        年份
     * @param month       月份
     * @param weekOfMonth 这个月的第几周
     * @param dayOfWeek   星期几
     * @return
     */
    public static String weekDateToData(int year, int month, int weekOfMonth, int dayOfWeek) {
        Calendar c = Calendar.getInstance();
        //计算出 x年 y月 1号 是星期几
        c.set(year, month - 1, 1);

        //如果i_week_day =1 的话 实际上是周日
        int i_week_day = c.get(Calendar.DAY_OF_WEEK);

        int sumDay = 0;
        //dayOfWeek+1 就是星期几(星期日 为 1)
        if (i_week_day == 1) {
            sumDay = (weekOfMonth - 1) * 7 + dayOfWeek + 1;
        } else {
            sumDay = 7 - i_week_day + 1 + (weekOfMonth - 1) * 7 + dayOfWeek + 1;
        }
        //在1号的基础上加上相应的天数
        c.set(Calendar.DATE, sumDay);
        SimpleDateFormat sf2 = new SimpleDateFormat("yyyy-MM-dd");
        return sf2.format(c.getTime());
    }

    /**
     * 获得当天之后的N天日期
     *
     * @param num 当天后的第N天
     * @return 返回的日期
     */
    public static List<String> getNextDays(int num) {
        List<String> list = new ArrayList<String>();
        SimpleDateFormat sdf = new SimpleDateFormat("YYYY-MM-DD");
        for (int i = 1; i < num; i++) {
            list.add(sdf.format(getAfterDate(i)));
        }
        return list;
    }

    /**
     * 获取当前日期n天后的日期
     *
     * @param n:返回当天后的第N天
     * @return 返回的日期
     */
    public static Date getAfterDate(int n) {
        Calendar c = Calendar.getInstance();
        c.add(Calendar.DAY_OF_MONTH, n);
        return c.getTime();
    }

    /**
     * 获取当前日期n天后的日期
     *
     * @param n:返回当天后的第N天
     * @return 返回的日期
     */
    public static Date getAfterDate(Date date, int n) {
        Calendar calendar = new GregorianCalendar();
        calendar.setTime(date);
        calendar.add(Calendar.DAY_OF_MONTH, n);
        return calendar.getTime();
    }

    /**
     * 将util.date转换成XMLGregorianCalendar
     * */
//    @SuppressWarnings("static-access")
//	public static XMLGregorianCalendar getXMLGregorianCalendar(Date date) throws Exception {
//	    Calendar calendar = Calendar.getInstance();
//	    calendar.setTime(date);
//	    DatatypeFactory dtf = DatatypeFactory.newInstance();
//	    return dtf.newXMLGregorianCalendar(
//	    calendar.get(calendar.YEAR),
//	    calendar.get(calendar.MONTH)+1,
//	    calendar.get(calendar.DAY_OF_MONTH),
//	    calendar.get(calendar.HOUR),
//	    calendar.get(calendar.MINUTE),
//	    calendar.get(calendar.SECOND),
//	    calendar.get(calendar.MILLISECOND),
//	    calendar.get(calendar.ZONE_OFFSET)/(1000*60));
//    }

    /**
     * 计算两日期间的天数
     *
     * @param startDate 开始日期
     * @param endDate   结束日期
     * @param tabType   传入的日期类型(天-dw,周-ww,月-mw)
     * @return
     */
    public static int getMidFromDoubleDate(String startDate, String endDate, String tabType) {
        Date date1 = null;
        Date date2 = null;
        if ("ww".equals(tabType)) {
            date1 = parseDate(startDate, ISO_WEEK_FORMAT);
            date2 = parseDate(endDate, ISO_WEEK_FORMAT);
        } else if ("mw".equals(tabType)) {
            date1 = parseDate(startDate, ISO_DATE_T_FORMAT);
            date2 = parseDate(endDate, ISO_DATE_T_FORMAT);
        } else {
            date1 = parseDate(startDate);
            date2 = parseDate(endDate);
        }
        return (int) (Math.abs((date1.getTime() - date2.getTime()) / (1000 * 60 * 60 * 24)) + 1);
    }

    /**
     * 根据指定格式解析日期
     *
     * @param date 日期字符串
     * @param fmt  格式
     * @return
     */
    public static Date parseDate(String date, String fmt) {
        try {
            Date d = new SimpleDateFormat(fmt).parse(date);
            Calendar c = Calendar.getInstance();
            c.setTime(d);
            return d;
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return null;
    }

    public static Date parseDate(String date) {
        return parseDate(date, ISO_DATE_NO_HYPHEN_FORMAT);
    }

}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值