# 时间工具类、常用时间转换TimeUtils

时间工具类、常用时间转换TimeUtils

  • 废话少说,上简单的代码
/**
 * @author LiDong
 * @description 时间工具类
 */
public class TimeUtils {

    private TimeUtils() {
    }

    private static final Logger logger = LoggerFactory.getLogger(TimeUtils.class);

    /**
     * 标准24小时日期格式
     */
    private static final String STANDARD_DATETIME_24 = "yyyy-MM-dd HH:mm:ss";

    /**
     * 标准12小时日期格式
     */
    private static final String STANDARD_DATETIME_12 = "yyyy-MM-dd hh:mm:ss";


    private static final String STANDARD_DATE = "yyyy-MM-dd";

    /**
     * 获得当前的时间戳
     *
     * @return
     */
    public static long currentTimestamp() {
        return System.currentTimeMillis();
    }

    /**
     * 获得当前标准时间 24小时制
     *
     * @return
     */
    public static String getDateTime24() {
        Date date = new Date();
        SimpleDateFormat simpleFormatter = new SimpleDateFormat(STANDARD_DATETIME_24);
        return simpleFormatter.format(date);
    }

    /**
     * 获得当前标准时间 12小时制
     *
     * @return
     */
    public static String getDateTime12() {
        Date date = new Date();
        SimpleDateFormat simpleFormatter = new SimpleDateFormat(STANDARD_DATETIME_12);
        return simpleFormatter.format(date);
    }


    /**
     * 格式化传入的字符串类型的时间:2020-10-12 20:51:34.0000
     *
     * @param str
     * @return
     */
    public static String parseDateTime(String str) {
        SimpleDateFormat simpleDateFormat;
        if (str.contains(":")) {
            simpleDateFormat = new SimpleDateFormat(STANDARD_DATETIME_24);
        } else {
            simpleDateFormat = new SimpleDateFormat(STANDARD_DATE);
        }
        try {
            Date date = simpleDateFormat.parse(str);
            return simpleDateFormat.format(date);
        } catch (ParseException e) {
            logger.error("paraseDateTime() error occur :{}", e.getMessage());
            return null;
        }
    }

    /**
     * String 类型的时间转换为 Date 类型的时间
     *
     * @param dateTime
     * @return
     */
    public static Date parseStrTime(String dateTime) {
        SimpleDateFormat simpleDateFormat;
        if (dateTime.contains(":")) {
            simpleDateFormat = new SimpleDateFormat(STANDARD_DATETIME_24);
        } else {
            simpleDateFormat = new SimpleDateFormat(STANDARD_DATE);
        }
        try {
            return simpleDateFormat.parse(dateTime);
        } catch (ParseException e) {
            logger.error("covertStrTime() error occur :{}", e.getMessage());
            return null;
        }
    }

    /**
     * 将时间戳转换为标准时间:yyyy-MM-dd HH:mm:ss
     *
     * @param timestamp
     * @return
     */
    public static String timeStampleToTime(String timestamp) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(STANDARD_DATETIME_24);
        long time = Long.parseLong(timestamp);
        Date date = new Date(time);
        return simpleDateFormat.format(date);
    }

    /**
     * 将时间字符串转换为
     *
     * @param str
     * @return
     */
    public static String timeTotimestamp(String str) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(STANDARD_DATETIME_24);
        try {
            Date date = simpleDateFormat.parse(str);
            long time = date.getTime();
            return String.valueOf(time);
        } catch (ParseException e) {
            logger.error("timeTotimestamp() error occur:{}", e.getMessage());
            return null;
        }
    }


    /**
     * 计算出当前时间的前或者后几天的时间,
     * 例如:当前时间的后5天的时间为:2020-10-17 21:42:35
     * 当前时间的前5天的时间为:2020-10-07 21:44:08
     *
     * @param num 今天开始的 前num天或者后num天
     * @return
     */
    public static String getWantDateTime(int num) {
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.DATE, num);
        Date date = calendar.getTime();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(STANDARD_DATETIME_24);
        return simpleDateFormat.format(date);
    }

    /**
     * 计算当前时间是 一年中的第几天 yy-MM-dd HH:mm:ss
     *
     * @param str yy-MM-dd HH:mm:ss的时间
     * @return
     */
    public static String getOrderInYear(String str) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(STANDARD_DATETIME_24);
        try {
            Date date = simpleDateFormat.parse(str);
            Calendar cal = Calendar.getInstance();
            cal.setTime(date);
            int days = getOrderDay(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH) + 1, cal.get(Calendar.DATE));
            return String.valueOf(days);
        } catch (ParseException e) {
            logger.error("getWhenDay() error occur :{}", e.getMessage());
            logger.error("请检查入参,样例入参为 yy-MM-dd HH:mm:ss");
            return null;
        }
    }

    /**
     * 计算 当前参数 时间是一年中的第几天
     *
     * @param year  年
     * @param month 月
     * @param day   日
     * @return
     */
    public static int getOrderDay(int year, int month, int day) {
        // 定义累加器储存天数
        int num = 0;
        // 遍历月份,求每个月份的天数和
        for (int i = 1; i < month; i++) {
            switch (i) {
                // 当月为大月时累加31
                case 1:
                case 3:
                case 5:
                case 7:
                case 8:
                case 10:
                case 12:
                    num += 31;
                    break;
                // 当月为二月时闰年累加29,平年累加28
                case 2:
                    num += (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) ? 29 : 28;
                    break;
                default:
                    num += 30;
                    break;
            }
        }
        // 加上日
        num += day;
        return num;
    }

}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

全栈程序员

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

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

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

打赏作者

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

抵扣说明:

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

余额充值