日期工具类

public class DateUtils {

    private static final String DEFAULT_DATE_FMT = "yyyy-MM-dd HH:mm:ss";

    /**
     * 得到当前时间戳, 秒
     *
     * @return
     */
    public static Long nowTimeStamp() {
        return LocalDateTime.now().toEpochSecond(ZoneOffset.of("+8"));
    }

    /**
     * 得到当前时间戳, 毫秒
     *
     * @return
     */
    public static Long nowTimeStampMS() {
        return LocalDateTime.now().toInstant(ZoneOffset.of("+8")).toEpochMilli();
    }

    /**
     * 时间字符串解析
     *
     * @param strDate
     * @return
     */
    public static Date strParseDate(String strDate) {
        return strParseDate(strDate, DEFAULT_DATE_FMT);
    }

    /**
     * 时间字符串解析
     *
     * @param strDate
     * @param fmt
     * @return
     */
    public static Date strParseDate(String strDate, String fmt) {
        try {
            SimpleDateFormat sdf = new SimpleDateFormat(fmt);
            return sdf.parse(strDate);
        } catch (ParseException e) {
            log.error("SimpleDateFormat parse date error date is {}", strDate);
        }
        return null;
    }

    /**
     * 字符串转时间戳
     *
     * @param strDate
     * @param fmt
     * @return
     */
    public static Long strParse2TimeStamp(String strDate, String fmt) {
        Date date = strParseDate(strDate, fmt);
        return date.getTime() / 100;
    }

    /**
     * 获取当前时间字符串, 按照给定的格式
     *
     * @return
     */
    public static String getNowFmt(String pattern) {
        LocalDateTime nowLdt = LocalDateTime.now();
        DateTimeFormatter dtf = DateTimeFormatter.ofPattern(pattern);
        return dtf.format(nowLdt);
    }

    /**
     * 获取当前时间字符串, 按照给定的格式
     *
     * @return
     */
    public static String getNowPlus(String pattern, Long toAdd, ChronoUnit unit) {
        DateTimeFormatter dtf = DateTimeFormatter.ofPattern(pattern);
        return dtf.format(LocalDateTime.now().plus(toAdd, unit));
    }

    /**
     * 获取当前时间字符串, 按照给定的格式
     *
     * @return
     */
    public static Date getNowPlus(Long toAdd, ChronoUnit unit) {
        return Date.from(LocalDateTime.now().plus(toAdd, unit).toInstant(ZoneOffset.of("+8")));
    }

    /**
     * date -> 时间戳
     *
     * @param date
     * @return
     */
    public static Long date2TimeStamp(Date date) {
        return date.getTime() / 1000;
    }

    public static LocalDateTime timestamp2Datetime(long timestamp) {
        Instant instant = Instant.ofEpochMilli(timestamp);
        return LocalDateTime.ofInstant(instant, ZoneId.systemDefault());
    }

    public static Date ts2Date(long timestamp) {
        LocalDateTime localDateTime = timestamp2Datetime(timestamp * 1000);
        return Date.from(localDateTime.toInstant(ZoneOffset.of("+8")));
    }

    /**
     * 获取近半年前时间
     *
     * @param pattern
     * @return
     */

    public static Map<String, String> preHalfofYear(String pattern) {
        SimpleDateFormat dtf = new SimpleDateFormat(pattern);
        Calendar c = Calendar.getInstance();
        Map<String, String> map = new HashMap<>();
        c.setTime(new Date());
        map.put("afterMonth", dtf.format(c.getTime()));
        c.add(Calendar.MONTH, -6);
        map.put("preMonth", dtf.format(c.getTime()));
        return map;
    }

    /**
     * 获取给定时间段内 指定格式的所有日期天数
     */
    public static List<String> getNowFromTime(Date date, String pattern, int index) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat(pattern);
        List<String> list = new ArrayList<>();
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        list.add(sdf.format(c.getTime()));
        for (int i = 1; i < index; i++) {
            c.add(Calendar.DATE, -1);
            list.add(sdf.format(c.getTime()));
        }
        return list;
    }

    /**
     * 获取前几(flag)天的日期
     *
     * @param flag
     * @return
     */
    public static List<Date> getLast7Days(int flag) {
        List<Date> last7Days = new ArrayList<>();
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        last7Days.add(calendar.getTime());
        for (int i = 1; i < flag; i++) {
            calendar.add(Calendar.DATE, -1);
            last7Days.add(calendar.getTime());
        }
        return last7Days;
    }

    /**
     * 获取给定时间戳距当前时间差
     *
     * @param timestamp
     * @return
     */
    public static Long getNowTimeDiff(Long timestamp) {
        return nowTimeStamp() - timestamp;
    }

    /**
     * 获取24小时之内日期集合
     */

    public static List<String> get24Hours() {
        List<String> hours = new ArrayList<>();
        for (int i = 23; i >= 0; i--) {
            Calendar calendar = Calendar.getInstance();
            calendar.add(Calendar.HOUR_OF_DAY, -i);
            //获取当前秒
            SimpleDateFormat s = new SimpleDateFormat("H时");
            //格式化时间格式
            hours.add(s.format(calendar.getTime()));
        }
        return hours;
    }


    /**
     * 获取当天日期集合
     */

    public static List<String> getOneDay() {
        List<String> hours = new ArrayList<>();
        Calendar curTime = Calendar.getInstance();
        int hour = curTime.get(Calendar.HOUR_OF_DAY);
        for (int i = hour; i >= 0; i--) {
            Calendar calendar = Calendar.getInstance();
            calendar.add(Calendar.HOUR_OF_DAY, -i);
            //获取当前秒
            SimpleDateFormat s = new SimpleDateFormat("H时");
            //格式化时间格式
            hours.add(s.format(calendar.getTime()));
        }
        return hours;
    }


    /**
     * 日期转换成字符串
     *
     * @param date
     * @return str
     */
    public static String DateToStr(Date date) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        String str = format.format(date);
        return str;
    }


    /**
     * 日期转换成字符串
     *
     * @param date
     * @return str
     */
    public static String Date2Str(Date date) {
        SimpleDateFormat format = new SimpleDateFormat("MM-dd");
        String str = format.format(date);
        return str;
    }


    /**
     * 获取某个日期为星期几
     *
     * @param date
     * @return String "星期*"
     */
    public static String getDayWeekOfDate1(Date date) {
        String[] weekDays = {"星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"};
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);

        int w = cal.get(Calendar.DAY_OF_WEEK) - 1;
        if (w < 0)
            w = 0;

        return weekDays[w];
    }

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值