Java中当前时间的当月第一天,当月最后一天,当年的第一天,当年的最后一天

1.第一种实现方式(使用calendar类实现)

小结: 这里是利用 Calendar 日历类进行设置

1.1 获取当前时间所在月的第一天

    /**
     * 获取传入日期所在月的第一天
     *
     * @param date 时间
     * @return java.util.Date
     * @since 2021/9/11
     */
    private static Date getFirstDayDateOfMonth(final Date date) {
        final Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        final int last = cal.getActualMinimum(Calendar.DAY_OF_MONTH);
        cal.set(Calendar.DAY_OF_MONTH, last);
        Date time = cal.getTime();
        return processFirstDate(time);
    }

    /**
     * 处理时间,在日期后面,增加 00:00:00
     */
    private static Date processFirstDate(Date time) {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String firstDate = df.format(time);
        StringBuilder firstDateTimeStr = new StringBuilder().append(firstDate).append(" 00:00:00");
        Date firstDateTime = null;
        try {
            firstDateTime = sdf.parse(firstDateTimeStr.toString());
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return firstDateTime;
    }

测试结果:

    @Test
    private void testFirstDayOfMonth(){
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        // 获取当前时间
        long currentTimeMillis = System.currentTimeMillis();
        Date currentDate = new Date(currentTimeMillis);
        // 获取当前时间所在月的第一天
        Date firstDayDateOfMonth = getFirstDayDateOfMonth(currentDate);
        System.out.println("当前时间为:"+format.format(currentTimeMillis));
        System.out.println("当前时间为:"+format.format(firstDayDateOfMonth));
    }
}
    /**
     * 获取到当前时间
     */
    private Date getCurrentDate() {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        long currentTimeMillis = System.currentTimeMillis();
        Date currentDate = new Date(currentTimeMillis);
        System.out.println("当前时间为:" + format.format(currentDate));
        return currentDate;
    }
// 输出结果
//当前时间为:2021-09-28 09:12:03
//所在月第一天为:2021-09-01 00:00:00

1.2 获取当前时间所在月的最后一天

    /**
     * 获取传入日期所在月的最后一天
     *
     * @param date 时间
     * @return java.util.Date
     * @since 2021/9/11
     */
    private static Date getLastDayOfMonth(final Date date) {
        final Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        final int last = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
        cal.set(Calendar.DAY_OF_MONTH, last);
        Date time = cal.getTime();
        return processDate(time);
    }

    /**
     * 在时间后面增加 23:59:59
     *
     * @param time 时间
     * @return java.util.Date
     * @since 2021/9/27
     */
    private static Date processDate(Date time) {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String lastDate = df.format(time);
        StringBuilder lastDateTimeStr = new StringBuilder().append(lastDate).append(" 23:59:59");
        Date lastDateTime = null;
        try {
            lastDateTime = sdf.parse(lastDateTimeStr.toString());
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return lastDateTime;
    }

测试结果:

    /**
     *  获取当前时间所在月的最后一天
     */
    @Test
    void testLastDayOfMonth() {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        // 获取当前时间
        Date currentDate = getCurrentDate();
        // 获取当前时间所在月的最后一天
        Date lastDayOfMonth = getLastDayOfMonth(currentDate);
        System.out.println("所在月第一天为:" + format.format(lastDayOfMonth));
    }
    /**
     * 获取到当前时间
     */
    private Date getCurrentDate() {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        long currentTimeMillis = System.currentTimeMillis();
        Date currentDate = new Date(currentTimeMillis);
        System.out.println("当前时间为:" + format.format(currentDate));
        return currentDate;
    }
// 运行结果
//当前时间为:2021-09-28 09:17:38
//所在月第一天为:2021-09-30 23:59:59

1.3 获取当前时间所在年的第一天

    /***
     * 获取传入日期所在年的第一天
     *
     */
    public static Date getFirstDayDateOfYear(final Date date) {
        final Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        final int last = cal.getActualMinimum(Calendar.DAY_OF_YEAR);
        cal.set(Calendar.DAY_OF_YEAR, last);
        Date time = cal.getTime();
        return processFirstDate(time);
    }

    /**
     * 处理时间,在日期后面,增加 00:00:00
     */
    private static Date processFirstDate(Date time) {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String firstDate = df.format(time);
        StringBuilder firstDateTimeStr = new StringBuilder().append(firstDate).append(" 00:00:00");
        Date firstDateTime = null;
        try {
            firstDateTime = sdf.parse(firstDateTimeStr.toString());
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return firstDateTime;
    }

测试结果:

    /**
     * 获取当前时间所在年的第一天
     */
    @Test
    void testFirstDayOfYear(){
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        // 获取当前时间
        Date currentDate = getCurrentDate();
        Date firstDayDateOfYear = getFirstDayDateOfYear(currentDate);
        System.out.println("所在月第一天为:" + format.format(firstDayDateOfYear));
    }
    /**
     * 获取到当前时间
     */
    private Date getCurrentDate() {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        long currentTimeMillis = System.currentTimeMillis();
        Date currentDate = new Date(currentTimeMillis);
        System.out.println("当前时间为:" + format.format(currentDate));
        return currentDate;
    }
// 输出
//当前时间为:2021-09-28 09:23:28
//所在月第一天为:2021-01-01 00:00:00

1.4 获取当前时间所在年的最后一天

    /**
     * 获取传入日期所在年的最后一天
     */
    private static Date getLastDayOfYear(final Date date) {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        final Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        final int last = cal.getActualMaximum(Calendar.DAY_OF_YEAR);
        cal.set(Calendar.DAY_OF_YEAR, last);
        Date time = cal.getTime();
        return processDate(time);
    }
    /**
     * 在时间后面增加 23:59:59
     *
     * @param time 时间
     * @return java.util.Date
     * @author HuangLongFei
     * @since 2021/9/27
     */
    private static Date processDate(Date time) {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String lastDate = df.format(time);
        StringBuilder lastDateTimeStr = new StringBuilder().append(lastDate).append(" 23:59:59");
        Date lastDateTime = null;
        try {
            lastDateTime = sdf.parse(lastDateTimeStr.toString());
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return lastDateTime;
    }

测试结果:

    /**
     * 获取当前时间所在年的最后一天
     */
    @Test
    void testLastDayOfYear(){
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        // 获取当前时间
        Date currentDate = getCurrentDate();
        Date lastDayOfYear = getLastDayOfYear(currentDate);
        System.out.println("所在年最后一天为:" + format.format(lastDayOfYear));
    }

    /**
     * 获取到当前时间
     */
    private Date getCurrentDate() {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        long currentTimeMillis = System.currentTimeMillis();
        Date currentDate = new Date(currentTimeMillis);
        System.out.println("当前时间为:" + format.format(currentDate));
        return currentDate;
    }
// 运行结果
// 当前时间为:2021-09-28 09:28:36
// 所在年最后一天为:2021-12-31 23:59:59

2.第二种实现方式(推荐,LocalDateTime类)

2.1 获取当前时间的月初,月末,年初,年末

    /**
     * 传入时间,获取到传入时间所在月的第一天
     * 使用 Java8,中的 LocalDateTime 类
     *
     * @param localDateTime localDateTime
     * @return java.time.LocalDateTime
     * @since 2021/9/27
     */
    private static LocalDateTime getFirstDayOfMonth(LocalDateTime localDateTime) {
        return localDateTime.with(TemporalAdjusters.firstDayOfMonth()).withHour(0).withMinute(0).withSecond(0);
    }

    /**
     * 获取传入时间所在月的最后一天
     *
     * @param localDateTime localDateTime
     * @return java.time.LocalDateTime
     * @since 2021/9/27
     */
    private static LocalDateTime getLastDayOfMonth(LocalDateTime localDateTime) {
        return localDateTime.with(TemporalAdjusters.lastDayOfMonth()).withHour(23).withMinute(59).withSecond(59);
    }

    /**
     * 获取传入时间所在年的第一天
     *
     * @param localDateTime localDateTime
     * @return java.time.LocalDateTime
     * @since 2021/9/27
     */
    private static LocalDateTime getFirstDayOfYear(LocalDateTime localDateTime) {
        return localDateTime.with(TemporalAdjusters.firstDayOfYear()).withHour(0).withMinute(0).withSecond(0);
    }

   /**
     * 获取传入时间所在年的最后一天
     *
     * @param localDateTime localDateTime
     * @return java.time.LocalDateTime
     * @since 2021/9/27
     */
    private static LocalDateTime getLastDayOfYear(LocalDateTime localDateTime) {
        return localDateTime.with(TemporalAdjusters.lastDayOfYear()).withHour(23).withMinute(59).withSecond(59);
    }
  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值