2021-10-25⭐时间处理类⭐

本文分享了一款实用的日期处理工具类,能快速解决项目中常见的时间需求,包括加减秒、分钟、小时、天等,还提供灵活的时间格式转换和字符串解析功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

前几天,在项目中一直遇到处理时间的需求,搞得我要一直搜,有时候搜还不太准确。我一气之下,写了一个比较全的日期处理工具,基本上什么需求都可以。

大家可以将其放在 Util 层中,充当一个工具类。

我都是用 SE 写的,调用十分简单。

package cn.com.wind.other;

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

public class TimeHelper {

    public static void main(String[] args) throws ParseException {
        System.out.println("当前时间点为    :" + getTime());
        System.out.println("前5秒的时间点为 :" + getPreSecond(5));
        System.out.println("前5分钟的时间点为:" + getPreMinute(5));
        System.out.println("前5小时的时间点为:" + getPreHour(5));
        System.out.println("前5天的时间点为:" + getPreDay(5));
        System.out.println("前5月的时间点为:" + getPreMonth(5));
        System.out.println("前5年的时间点为:" + getPreYear(5));

        System.out.println("2021-10-12 15:23:42 时间节点前5分钟的时间为:" + getFixTime("2021-10-12 15:23:42", 5));
        System.out.println("获取当前分钟: " + getMinute());
        System.out.println("时间 20210210 转换之后的时间为: " + timeFormatConvert("20210210", "yyyyMMdd", "yyyy-MM-dd HH:mm:ss"));
        System.out.println("String 类型转换成时间为: " + strConvertTime("2021-10-12 15:23:42", "yyyy-MM-dd HH:mm:ss"));

        System.out.println(Arrays.toString(getListDays("2021-10-05 15:23:42", "2021-10-12 16:21:40", "yyyy-MM-dd HH:mm:ss").toArray()));

    }

    /**
     * 获取当前分钟
     * @return
     */
    public static int getMinute() {
        return Calendar.getInstance().get(Calendar.MINUTE);
    }

    /**
     * 列举出时间端内的所有时间,以天为演示单位
     * @return
     */
    public static List<String> getListDays(String beginTime, String endTime, String format) throws ParseException {
        List<String> days = new ArrayList<>();  //返回日期的集合
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);
        Date beginDate = simpleDateFormat.parse(beginTime);
        Date endDate = simpleDateFormat.parse(endTime);

        Calendar beginCalendar = Calendar.getInstance();
        beginCalendar.setTime(beginDate);
        Calendar endCalendar = Calendar.getInstance();
        endCalendar.setTime(endDate);

        while(beginCalendar.before(endCalendar)) {  //注意这里不要搞成死循环
            days.add(simpleDateFormat.format(beginCalendar.getTime()));
            beginCalendar.add(Calendar.DAY_OF_MONTH, 1);
        }
        return days;
    }

    /**
     * 时间格式转换方法
     * @param time 时间
     * @param srcFormat 源时间格式
     * @param destFormat 目标时间格式
     */
    public static String timeFormatConvert(String time, String srcFormat, String destFormat) throws ParseException {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(srcFormat);
        long longTime = simpleDateFormat.parse(time).getTime();  //将时间转化为时间戳
        return new SimpleDateFormat(destFormat).format(longTime);
    }

    /**
     * 将string类型转换为时间
     * @param time
     * @param format
     */
    public static Date strConvertTime(String time, String format) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        Date date = sdf.parse(time);
        return date;
    }

    /**
     * 获取某个时间节点前几分钟时间,可以自己设置时间节点。
     * @param time 自定义时间节点
     * @param reviseTime 调整几分钟
     */
    public static String getFixTime(String time, int reviseTime) throws ParseException {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        long longTime = simpleDateFormat.parse(time).getTime();
        Date date = new Date(longTime);  //创建一个时间日期
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.MINUTE, -reviseTime);  //调用 API 修改时间
        date = calendar.getTime();  //重新赋值时间
        return simpleDateFormat.format(date);
    }

    /**
     * 获取当前时间
     */
    public static String getTime(){
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = new Date();
        return simpleDateFormat.format(date);
    }

    /**
     * 获取当前几秒的时间
     * @param preSecond
     * @return
     */
    public static String getPreSecond(int preSecond) {
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.SECOND, -preSecond);
        Date date = calendar.getTime();
        return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(date);
    }

    /**
     * 获取当前几分钟的时间
     * @param preMinute
     * @return
     */
    public static String getPreMinute(int preMinute) {
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.MINUTE, -preMinute);
        Date date = calendar.getTime();
        return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(date);
    }

    /**
     * 获取当前几小时的时间
     * @param preHour
     * @return
     */
    public static String getPreHour(int preHour) {
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.HOUR, -preHour);
        Date date = calendar.getTime();
        return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(date);
    }

    /**
     * 获取当前几天的时间
     * @param preDay
     * @return
     */
    public static String getPreDay(int preDay) {
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.DAY_OF_MONTH, -preDay);
        Date date = calendar.getTime();
        return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(date);
    }

    /**
     * 获取当前几月的时间
     * @param preMonth
     * @return
     */
    public static String getPreMonth(int preMonth) {
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.MONTH, -preMonth);
        Date date = calendar.getTime();
        return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(date);
    }

    /**
     * 获取当前几年的时间
     * @param preYear
     * @return
     */
    public static String getPreYear(Integer preYear) {
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.YEAR, -preYear);
        Date date = calendar.getTime();
        return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(date);
    }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值