DateUtils

DateUtils

package com.common.utils;

import cn.hutool.core.date.DatePattern;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * 类 名: DateUtils
 * 描 述:时间格式工具类
 * 作 者:江湖@小小白
 * 创 建:2022/3/7 17:27
 */
public class DateUtils {
    private static final Logger logger = LoggerFactory.getLogger(DateUtils.class);

    /**
     * 默认日期时间格式
     */
    public static final String DEFAULT_DATE_TIME_FORMAT = "yyyy-MM-dd HH:mm:ss";
    /**
     * 默认日期格式
     */
    public static final String DEFAULT_DATE_FORMAT = "yyyy-MM-dd";
    /**
     * 简易日期格式
     */
    public static final String SIMPLE_DATE_FORMAT = "yyyyMMdd";
    /**
     * 默认时间格式
     */
    public static final String DEFAULT_TIME_FORMAT = "HH:mm:ss";

    /**
     * 中文时间日期格式
     */
    public static final String CHINESE_DATE_TIME_FORMAT = "yyyy年MM月dd日 HH:mm";

    /**
     * 8位字符串时间格式 转为 10位的横杠时间格式
     * 例如:20211211 转为 2021-12-11
     * @param str_date
     * @return
     */
    public static String str8Formatstr10(String str_date)  {
        String date = "";
        if (StringUtils.isBlank(str_date) || str_date.length() != 8) {
            return "";
        }
        SimpleDateFormat parseFormat = new SimpleDateFormat("yyyyMMdd");
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        try {
           Date parseDate = parseFormat.parse(str_date);
           date = dateFormat.format(parseDate);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date;
    }

    /**
     * 8位字符串时间格式 转为 10位的斜杠时间格式
     * 例如:20211211  转为 2021/12/11
     * @param str_date
     * @return
     */
    public static String str8Formatstr10Sprit(String str_date){
        String date = "";
        if (StringUtils.isBlank(str_date) || str_date.length() != 8) {
            return "";
        }
        SimpleDateFormat parseFormat = new SimpleDateFormat("yyyyMMdd");
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd");
        try {
            Date parseDate = parseFormat.parse(str_date);
            date = dateFormat.format(parseDate);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date;

    }

    /**
     * 8位日期格式的字符串 转化为  Date类型
     * 例如:20211211 转为 Date;
     * @param str_date
     * @return
     */
    public static Date str8FormatDate(String str_date){
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd");
        Date date = null;
        if (StringUtils.isBlank(str_date) || str_date.length() != 8) {
            return null;
        }
        try {
            date = dateFormat.parse(str_date);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date;

    }
    
    /**
     * 10位日期格式的字符串 转化为  Date类型
     * 例如:2021-12-11 转为 Date;
     * @param str_date
     * @return
     */
    public static Date str10FormatDate(String str_date){
    	SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
    	Date date = null;
    	if (StringUtils.isBlank(str_date) || str_date.length() != 10) {
    		return null;
    	}
    	try {
    		date = dateFormat.parse(str_date);
    	} catch (ParseException e) {
    		e.printStackTrace();
    	}
    	return date;
    	
    }

    /**
     * 8位字符串日期转为6位字符串
     * 例如:20211112 转为 11-12
     * @param str_date
     * @return
     */
    public static String str8Formatstr6(String str_date){
        if (StringUtils.isBlank(str_date) || str_date.length() != 8) {
            return "";
        }
        String dateStr = "";
        SimpleDateFormat parseFormat = new SimpleDateFormat("yyyyMMdd");
        SimpleDateFormat dateFormat = new SimpleDateFormat("MM-dd");
        try {
            Date parse = parseFormat.parse(str_date);
            dateStr = dateFormat.format(parse);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return dateStr;
    }


    /**
     * 将Date类型的字段转化为10位横杠分割的日期字符串
     * 例如:Date类型的字段 转为 2021-10-10
     * @param date
     * @return
     */
    public static String dateFormatstr10(Date date){
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        return dateFormat.format(date);

    }

    /**
     * 将Date类型的字段转化为MM月dd日 HH:mm
     * @param date
     * @return
     */
    public static String dateFormatstr11(Date date){
        SimpleDateFormat dateFormat = new SimpleDateFormat("MM月dd日 HH:mm");
        return dateFormat.format(date);
    }

    /**
     * 判断当前日期是星期几
     * @return
     */
    public static String dateOfWeek(Date date) {
        String[] weekDaysName = {"星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"};
        Calendar instance = Calendar.getInstance();
        instance.setTime(date);
        int index = instance.get(Calendar.DAY_OF_WEEK) - 1;
        return weekDaysName[index];
    }

    /**
     * 判断当前是否是15点以后,确定交易是当天还是明天,然后计算加几天之后是否是工作日
     * @param now 当前时间
     * @param step 加几天
     * @param step 工作日的时间集合
     */
    public static String dateAddStepIfWorkDay(Date now, Integer step,List<String> list) {

        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        //判断交易日
        Date tradeDay = now;
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(now);
        int i = calendar.get(Calendar.HOUR_OF_DAY);
        if (i > 15) {
            calendar.set(Calendar.DAY_OF_MONTH, calendar.get(Calendar.DAY_OF_MONTH) + 1);
            tradeDay = calendar.getTime();
        }

        // 获取交易发生的第一工作日时间
        Date workDay = null;
        while (true) {
            if (list.contains(simpleDateFormat.format(tradeDay))) {
                workDay = tradeDay;
                break;
            } else {
                calendar.set(Calendar.DAY_OF_MONTH, calendar.get(Calendar.DAY_OF_MONTH) + 1);
                tradeDay = calendar.getTime();
            }
        }

        // 获取 T+N 的工作日
        if (workDay == null) {
            return "不存在满足条件的工作日";
        } else {
            Calendar instance = Calendar.getInstance();
            instance.setTime(workDay);
            Date workStepDay = null;
            int count = 0;
            int num = 0;
            while (true) {
                num++;
                instance.set(Calendar.DAY_OF_MONTH, instance.get(Calendar.DAY_OF_MONTH) + 1);
                workStepDay = instance.getTime();

                if (list.contains(simpleDateFormat.format(workStepDay))) {
                    count++;
                }

                if (count == step) {
                    return simpleDateFormat.format(workStepDay);
                }

                //最多循环365一次,也就是一年,若找不到就返回提示语
                if (num == 365) {
                    return "不存在满足T+N的工作日";
                }

            }

        }


    }

    /**
     * 19位日期格式的字符串 转化为  Date类型
     * 例如:2021-12-11 10:12:12 转为 Date;
     * @param str_date
     * @return
     */
    public static Date str19FormatDate(String str_date){
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = null;
        if (StringUtils.isBlank(str_date) || str_date.length() != 19) {
            return null;
        }
        try {
            date = dateFormat.parse(str_date);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date;

    }

    /**
     * 将Date类型的字段转化为 8位日期字符串
     * 例如:Date类型的字段 转为 20191010
     * @param date
     * @return
     */
    public static String dateFormatstr8(Date date){
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd");
        return dateFormat.format(date);

    }

    /**
     * 将Date类型的字段转化为 19位日期字符串
     * 例如:Date类型的字段 转为 2021-10-10 l0:10:10
     * @param date
     * @return
     */
    public static String dateFormatstr19(Date date){
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return dateFormat.format(date);

    }

    /**
     * 将Date类型的字段转化为 5位日期字符串
     * 例如:Date类型的字段 转为 10-10
     * @param date
     * @return
     */
    public static String dateFormatstr5(Date date){
        SimpleDateFormat dateFormat = new SimpleDateFormat("MM-dd");
        return dateFormat.format(date);

    }
    /**
     * 将Date类型的字段转化为MM月dd日
     * @param date
     * @return
     */
    public static String dateFormatstr6(Date date){
        SimpleDateFormat dateFormat = new SimpleDateFormat("MM月dd日");
        return dateFormat.format(date);
    }

    /**
     * 8位字符串时间格式 转为 8位的横杠时间格式
     * 例如:20211211 转为 21-12-11
     * @param str_date
     * @return
     */
    public static String str8Formatstr8(String str_date)  {
        String date = "";
        if (StringUtils.isBlank(str_date) || str_date.length() != 8) {
            return "";
        }
        SimpleDateFormat parseFormat = new SimpleDateFormat("yyyyMMdd");
        SimpleDateFormat dateFormat = new SimpleDateFormat("yy-MM-dd");
        try {
            Date parseDate = parseFormat.parse(str_date);
            date = dateFormat.format(parseDate);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date;
    }

    /**
     * 8位字符串时间格式 转为 10位的年月日时间格式,月和日以0开头就去掉0,2019年9月9日
     * 例如:20211211 转为 21-12-11
     * @param dateStr
     * @return
     */
    public static String str8Formatstr10YMD(String dateStr)  {
        String date = "";
        if (StringUtils.isBlank(dateStr) || dateStr.length() != 8) {
            return "";
        }
        SimpleDateFormat parseFormat = new SimpleDateFormat("yyyyMMdd");
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy年M月d日");
        try {
            Date parseDate = parseFormat.parse(dateStr);
            date = dateFormat.format(parseDate);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date;
    }

    /**
     * 用指定格式转换
     * @param dateStr
     * @return
     */
    public static String strToStr(String dateStr, String formatOri, String formatTar)  {
        String date = "";
        SimpleDateFormat parseFormat = new SimpleDateFormat(formatOri);
        SimpleDateFormat dateFormat = new SimpleDateFormat(formatTar);
        try {
            Date parseDate = parseFormat.parse(dateStr);
            date = dateFormat.format(parseDate);
        } catch (Exception e) {
            date = "";
        }
        return date;
    }

    /**
     * 19位日期格式的字符串 转化为  Date类型
     * 例如:2021-12-11 12:12:12 转为 Date;
     * @param str_date
     * @return
     */
    public static Date str17FormatDate(String str_date){
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd HH:mm:ss");
        Date date = null;
        if (StringUtils.isBlank(str_date) || str_date.length() != 17) {
            return null;
        }
        try {
            date = dateFormat.parse(str_date);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date;

    }


    /**
     * 10位日期格式的字符串 转化为  Date类型
     * 例如:2021.12.11 转为 Date;
     * @param str_date
     * @return
     */
    public static Date strPoint10FormatDate(String str_date){
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy.MM.dd");
        Date date = null;
        if (StringUtils.isBlank(str_date) || str_date.length() != 10) {
            return null;
        }
        try {
            date = dateFormat.parse(str_date);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date;

    }

    /**
     * Timestamp转String
     * @param time
     * @param formatStr
     * @return
     */
    public static String timeStampToString(Timestamp time,String formatStr){
        SimpleDateFormat dateFormat = new SimpleDateFormat(formatStr);
        return dateFormat.format(time);
    }

    /**
     * 10位字符串时间格式 转为 8位的时间格式
     * 例如:2021-12-11 转为 20211211
     * @param str_date
     * @return
     */
    public static String str10Formatstr8(String str_date)  {
        String date = "";
        if (StringUtils.isBlank(str_date) || str_date.length() != 10) {
            return "";
        }
        SimpleDateFormat parseFormat = new SimpleDateFormat("yyyy-MM-dd");
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd");
        try {
            Date parseDate = parseFormat.parse(str_date);
            date = dateFormat.format(parseDate);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date;
    }

    /**
     * 10位字符串时间格式 转为 5位的时间格式
     * 例如:2021-12-11 转为 12-11
     * @param str_date
     * @return
     */
    public static String str10Formatstr5(String str_date)  {
        String date = "";
        if (StringUtils.isBlank(str_date) || str_date.length() != 10) {
            return "";
        }
        SimpleDateFormat parseFormat = new SimpleDateFormat("yyyy-MM-dd");
        SimpleDateFormat dateFormat = new SimpleDateFormat("MM-dd");
        try {
            Date parseDate = parseFormat.parse(str_date);
            date = dateFormat.format(parseDate);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date;
    }
    
    /**
	 * 按照指定的格式把字符串转换成时间,格式的写法类似yyyy.MM.dd HH:mm:ss.SSS
	 * @param str 字符串
	 * @param pattern 格式
	 * @return 时间
	 */
	public static Date string2Date(String str, String pattern) {
		if (FormatCheck.isEmpty(str)) {
			return null;
		}

		SimpleDateFormat dateFormat = new SimpleDateFormat(pattern);
		Date date = null;
		try {
			date = dateFormat.parse(str);
		} catch (ParseException e) {
			// ignore
		}
		return date;
	}


    /**
     * @Description 获取当前日期年月日
     * @Param []
     * @Author zhangting
     * @Date 10:49 2021/5/26
     * @return java.util.Date
     **/
    public static Date getCurrentYMD(){
        Calendar ca = Calendar.getInstance();
        ca.set(Calendar.HOUR_OF_DAY, 0);
        ca.set(Calendar.MINUTE, 0);
        ca.set(Calendar.SECOND, 0);
        return ca.getTime();
    }

    /**
     * 时间格式转为自定字符串
     * @param time
     * @param pattern
     * @return
     */
    public static String getLocalTimeStr(LocalTime time,String pattern){
        if (time == null){
            return StringUtils.EMPTY;
        }
        if (StringUtils.isBlank(pattern)){
            pattern = DEFAULT_TIME_FORMAT;
        }
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern(pattern);
        return time.format(formatter);
    }
    /**
     * 时间格式时间字符串
     * @param time
     * @return
     */
    public static String getLocalTimeStr(LocalTime time){
        return getLocalTimeStr(time,DEFAULT_TIME_FORMAT);
    }

    /**
     * 格式化为中文日期格式,则返回类似:2018年10月24日HH时mm分ss秒
     * @param dateTime 被格式化的日期
     * @return 中文日期字符串
     */
    public static String formatChineseDate(LocalDateTime dateTime) {
        if (null == dateTime) {
            return null;
        }
        return DatePattern.CHINESE_DATE_TIME_FORMAT.format(dateTime);
    }

    /**
     * 指定时间字符串返回
     * @param dateTime
     * @param pattern
     * @return
     */
    public static String formatLocalDateTime(LocalDateTime dateTime,String pattern){
        if (dateTime == null){
            return StringUtils.EMPTY;
        }
        if (StringUtils.isBlank(pattern)){
            pattern = DEFAULT_DATE_TIME_FORMAT;
        }
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern(pattern);
        return dateTime.format(formatter);
    }

    /**
     * 标准日期时间格式转换
     * @param dateTime
     * @return
     */
    public static String formatLocalDateTime(LocalDateTime dateTime){
        return formatLocalDateTime(dateTime,DEFAULT_DATE_TIME_FORMAT);
    }

    /**
     * @Description 日期和星期 5月29日(周日)
     * @Param []
     * @Author zhangting
     * @Date 17:31 2021/12/29
     * @return java.lang.String
     **/
    public static String getDate6AndWeek(Date date){
        String[] weekDays = {"周日", "周一", "周二", "周三", "周四", "周五", "周六"};
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return new StringBuilder(dateFormatstr6(date)).append("(").append(weekDays[calendar.get(Calendar.DAY_OF_WEEK)-1]).append(")") .toString();
    }

    /**
     * @Description 返回指定日期的前几天,返回格式:yyyy-MM-dd HH:mm:ss
     * @Param []
     **/
    public static String getDateBefore(Date date, int days){
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.DATE, calendar.get(Calendar.DATE) + days);
        return dateFormatstr19(calendar.getTime());
    }

    public static void main(String[] args) {
        System.out.println(getLocalTimeStr(LocalTime.of(12, 0, 0), "HH:mm"));
    }
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值