时间工具类1

 

时间工具类

package com.iktapp.common.utils;

import cn.hutool.core.date.DateTime;
import org.apache.commons.lang3.StringUtils;

import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.ZoneId;
import java.util.*;

public class DateUtil
{
    //锁对象
    private static final Object lockObj = new Object();

    //存放不同的日期模板格式的sdf的Map
    private static Map<String, ThreadLocal<SimpleDateFormat>> sdfMap = new HashMap<String, ThreadLocal<SimpleDateFormat>>();

    //缺省日期格式
    public static final String DATE_DEFAULT_FORMAT = "yyyy-MM-dd";
    public static final String DATE_DEFAULT_FORMATSS = "yyyyMMddhhmmss";
    //全日期格式
    public static final String DATE_FULL_FORMAT = "yyyy-MM-dd HH:mm:ss";
    //精简日期格式
    public static final String DATE_SIMPLE_FORMAT = "yyyyMMdd";

    public static final String DATE_FULL_FORMAT_CHINA = "yyyy年MM月dd日 HH:mm:ss";
    //全日期格式无秒
    public static final String DATE_NO_SS_FORMAT = "yyyy-MM-dd HH:mm";

    public static final String DATE_TZ_FORMAT = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'";

    public static final String DATE_CRON_FORMAT = "ss mm HH dd MM ? yyyy";

    public static final String DATE_FORMAT_NO_ = "yyyyMMdd";

    public static final String [] DAY_NAMES = { "星期日", "星期一", "星期二", "星期三", "星期四", "星期五","星期六" };


    /**
     *时间转字符串
     */
    public static String dateToString(Date date, String pattern) {
        if (pattern == null || "".equals(pattern)) {
            pattern = DATE_DEFAULT_FORMAT;
        }
        if (date != null) {
            return getSimpleDateFormat(pattern).format(date);
        } else {
            return null;
        }
    }

    public static String dateToString(Date date) {
        return dateToString(date, null);
    }

    /**
     *得到格式化对象
     */
    public static SimpleDateFormat getSimpleDateFormat(final String pattern) {
        ThreadLocal<SimpleDateFormat> t1 = sdfMap.get(pattern);
        if (t1 == null) {
            synchronized (lockObj) {
                t1 = new ThreadLocal<SimpleDateFormat>() {
                    @Override
                    protected SimpleDateFormat initialValue() {
                        return new SimpleDateFormat(pattern);
                    }
                };
                sdfMap.put(pattern, t1);
            }
        }
        return t1.get();
    }

    /**
     *字符串转时间
     */
    public static Date stringToDate(String date, String pattern){
        if (pattern == null || "".equals(pattern)) {
            pattern = DATE_DEFAULT_FORMAT;
        }
        try
        {
            return getSimpleDateFormat(pattern).parse(date);
        }
        catch (ParseException e)
        {
            e.printStackTrace();
        }
        return null;
    }

    /**
     *字符串转时间
     */
    public static Date stringToDate(String date) {
        Date result = null;
        if (date == null) {
            return null;
        }
        if (date.length() == DATE_DEFAULT_FORMAT.length()) {
            try {
                result = stringToDate(date, DATE_DEFAULT_FORMAT);
                return result;
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return null;
    }

    /**
     *获取当天日期
     */
    public static String getToday(String pattern) {
        return dateToString(new Timestamp(System.currentTimeMillis()), pattern);
    }

    /**
     * 将 yyyy-MM-dd'T'HH:mm:ss.SSS'Z' 类型的string类型转化为Date类型
     *
     * @param date 字符串
     * @return
     */
    public static Date tzToDate(String date) {
        DateFormat format = new SimpleDateFormat(DATE_TZ_FORMAT, Locale.CHINA);
        format.setTimeZone((TimeZone.getTimeZone("UTC")));
        try {
            return format.parse(date);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static Long diffTime(String ds1, String ds2) {
        DateFormat df = new SimpleDateFormat(DATE_FULL_FORMAT);
        try {
            Date d1 = df.parse(ds1);
            Date d2 = df.parse(ds2);
            long diff = d1.getTime() - d2.getTime();
            return diff;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 比较时间
     * @param date1
     * @param date2
     * @return
     */
    public static Long compare(Date date1, Date date2)
    {
        return date1.getTime()-date2.getTime();
    }

    public static Date getOneMonthEarlyDate(Date date)
    {
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        c.add(Calendar.MONTH, -1);
        c.add(Calendar.DATE, 1);
        return c.getTime();
    }

    /**
     * 将日期格式改为 yyyy-MM-dd 格式
     * @param date
     * @return
     */
    public static Date transformToYMD(Date date)
    {
        LocalDate localDate = date.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
        Date newDate = java.sql.Date.valueOf(localDate);
        return newDate;
    }

    /**
     * 获取日期所对的星期名
     * @param date
     * @return
     */
    public static String getDayName(Date date)
    {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK) - 1;
        return DAY_NAMES[dayOfWeek];
    }

    /**
     * 获取指定日期前后的日期, days为正数时,为指定日期后面的日期
     * @param date
     * @param days
     * @return
     */
    public static Date getDay(Date date, int days)
    {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.DATE, days);
        Date newDate = calendar.getTime();
        return newDate;
    }


    /**
     * 两个时间只差
     *
     * @param startDate
     * @param endDate
     * @return 秒数
     */
    public static Integer getBetweenSecond(Date startDate, Date endDate) {
        Integer seconds = 0;
        try {
            if (startDate != null && endDate != null) {
                long ss = 0;
                if (startDate.before(endDate)) {
                    ss = endDate.getTime() - startDate.getTime();
                } else {
                    ss = startDate.getTime() - endDate.getTime();
                }
                seconds = Integer.valueOf((int) (ss / (1000)));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return seconds;
    }

    /**
     * 获取YYYYMM格式日期
     *
     * @param date
     * @return
     */
    public static String getDateYYYYMM(Date date) {
        DateTime d = cn.hutool.core.date.DateUtil.date(date);
        return d.year() + StringUtils.leftPad("" + (d.month() + 1), 2, "0");
    }

    /**
     * @Author:
     * @Description:获取输入日期的前n天
     * @Date:
     * @strData:参数格式:yyyy-MM-dd
     * @return:返回格式:yyyy-MM-dd
     */
    public static String getPreDateByDate(String strData, int diff) {
        String preDate = "";
        Calendar c = Calendar.getInstance();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date date = null;
        try {
            date = sdf.parse(strData);
        } catch (java.text.ParseException e) {
            e.printStackTrace();
        }

        c.setTime(date);
        int day1 = c.get(Calendar.DATE);
        c.set(Calendar.DATE, day1 - diff);
        preDate = sdf.format(c.getTime());
        return preDate;
    }

    /**
     * 将date转为calendar
     * @param date
     * @return
     */
    public static Calendar getCalendar(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return calendar;
    }

    /**
     * 获取日期的年份
     * @param date
     * @return
     */
    public static int getYear(Date date) {
        return getCalendar(date).get(Calendar.YEAR);
    }

    /**
     * 获取日期的月份
     * @param date
     * @return
     */
    public static int getMonth(Date date) {
        return getCalendar(date).get(Calendar.MONTH) + 1;
    }


    /**
     * 判读时间差距,两个时间相差多少天,时,分,秒
     * @param date
     * @return
     */
    public static Long getDay(Date date) {
        Long days = null;
        try {
            //现在系统当前时间
            Date currentTime = new Date();
            //过去时间
            Date pastTime = date;
            long diff = currentTime.getTime() - pastTime.getTime();
            days = diff / (1000 * 60 * 60 * 24);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return days;
    }

}

 

posted @ 2023-06-19 22:26  xingmeng1  阅读(24)  评论(0)    收藏  举报