ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

java常用日期封装

2022-02-20 05:06:23  阅读:187  来源: 互联网

标签:封装 return 60 日期 static java now localDate public


1,再当前日期上进行累加

   /**
     * 当前日期加减多少天
     */
    public static String getNowDateString(int day){
        return LocalDate.now().plusDays(day).format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
    }

    /**
     * 当前日期加减多少月
     */
    public static Date getNowYmdString(int month) {
        return Date.from(LocalDate.now().plusMonths(month).atStartOfDay(ZoneOffset.ofHours(8)).toInstant());
    }

    /**
     * 当前日期加减多少年后凌晨的日期+时间字符串
     */
    public static String getBeginOfYear(int year) {
        LocalDate localDate = LocalDate.now().plusYears(year);
        LocalDateTime dateTime = LocalDateTime.of(localDate.getYear(), localDate.getMonth(), localDate.getDayOfMonth(), 0, 0, 0);
        return dateTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
    }

    /**
     * 当前日期加减多少天后凌晨的日期+时间字符串
     */
    public static String getBeginOfDay(int day) {
        LocalDate localDate = LocalDate.now().plusDays(day);
        LocalDateTime dateTime = LocalDateTime.of(localDate.getYear(), localDate.getMonth(), localDate.getDayOfMonth(), 0, 0, 0);
        return dateTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
    }

2,获取月初周初的时间

   /**
     * 获取周初第一天的时间字符串
     */
    public static String getFirstOfWeek() {
        LocalDateTime localDate = LocalDateTime.now().with(DayOfWeek.MONDAY);
        return localDate.format(DateTimeFormatter.ofPattern("yyyy-MM-dd 00:00:00"));
    }

    /**
     * 获取周末的时间字符串
     */
    public static String getEndOfWeek() {
        LocalDateTime localDate = LocalDateTime.now().with(DayOfWeek.SUNDAY);
        return localDate.format(DateTimeFormatter.ofPattern("yyyy-MM-dd 23:59:59"));
    }

    /**
     * 获取月初第一天的时间字符串
     */
    public static String getFirstOfMonth() {
        LocalDateTime localDate = LocalDateTime.now().with(TemporalAdjusters.firstDayOfMonth());
        return localDate.format(DateTimeFormatter.ofPattern("yyyy-MM-dd 00:00:00"));
    }

    /**
     * 获取月末的时间字符串
     */
    public static String getEndOfMonth() {
        LocalDateTime localDate = LocalDateTime.now().with(TemporalAdjusters.lastDayOfMonth());
        return localDate.format(DateTimeFormatter.ofPattern("yyyy-MM-dd 23:59:59"));
    }

    /**
     * 当前日期加减多少月后的月初日期时间戳
     */
    public static long getFirstOfMonth(int lastMonth) {
        return LocalDate.now()
                .minusMonths(lastMonth)
                .with(TemporalAdjusters.firstDayOfMonth())
                .atStartOfDay(ZoneId.systemDefault())
                .toInstant()
                .toEpochMilli();
    }

    /**
     * 当前日期加减多少月后的月尾日期时间戳
     */
    public static long getLastOfMonth(int lastMonth) {
        return LocalDate.now()
                .minusMonths(lastMonth)
                .with(TemporalAdjusters.firstDayOfNextMonth())
                .atStartOfDay(ZoneId.systemDefault())
                .toInstant()
                .toEpochMilli() - 1;
    }

3,获取日期区间内月份列表 ["2022-01","2022-02"]

   /**
     * 获取日期区间内月份列表 ["2022-01","2022-02"]
     */
    public static List<String> getRankingNeedMonth(String beginTime, String endTime) {
        List<String> ymList = Lists.newArrayList();
        int intervalMonth = (int)LocalDate.parse(endTime).until(LocalDate.parse(beginTime), ChronoUnit.MONTHS);
        if (0 == intervalMonth) {
            return ymList;
        }
        for (int i = intervalMonth; i < 0; i++) {
            ymList.add(LocalDate.now().plusMonths((i + 1)).format(DateTimeFormatter.ofPattern("yyyy-MM")));
        }
        return ymList;
    }

4,计算两个日期间隔了日,时,分,秒

    /**
     * 时间转换
     */
    public static Date str2Date(String time) {
        try {
            return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(time);
        } catch (ParseException e) {
            return new Date();
        }
    }

    /**
     * 计算参数与当前时间间隔多少天多少小时多少分钟多少秒
     */
    public static String calIntervalStr(String time) {
        Date now = new Date();
        Date before = str2Date(time);
        long diff = now.getTime() - before.getTime();
        long days = diff / (1000 * 60 * 60 * 24);
        long hours = (diff - days * (1000 * 60 * 60 * 24)) / (1000* 60 * 60);
        long minutes = (diff - days * (1000 * 60 * 60 * 24) - hours * (1000 * 60 * 60)) / (1000* 60);
        long second = (diff / 1000 - days * 24 * 60 * 60 - hours * 60 * 60 - minutes * 60);
        return days + "天" + hours + "小时" + minutes + "分钟" + second + "秒";
    }

标签:封装,return,60,日期,static,java,now,localDate,public
来源: https://blog.csdn.net/jjjackjack/article/details/122922854

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有