栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

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

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

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

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

1.1 获取当前时间所在月的第一天
    
    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);
    }

    
    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 获取当前时间所在月的最后一天
    
    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);
    }

    
    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);
    }

    
    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);
    }
    
    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 获取当前时间的月初,月末,年初,年末
    
    private static LocalDateTime getFirstDayOfMonth(LocalDateTime localDateTime) {
        return localDateTime.with(TemporalAdjusters.firstDayOfMonth()).withHour(0).withMinute(0).withSecond(0);
    }

    
    private static LocalDateTime getLastDayOfMonth(LocalDateTime localDateTime) {
        return localDateTime.with(TemporalAdjusters.lastDayOfMonth()).withHour(23).withMinute(59).withSecond(59);
    }

    
    private static LocalDateTime getFirstDayOfYear(LocalDateTime localDateTime) {
        return localDateTime.with(TemporalAdjusters.firstDayOfYear()).withHour(0).withMinute(0).withSecond(0);
    }

   
    private static LocalDateTime getLastDayOfYear(LocalDateTime localDateTime) {
        return localDateTime.with(TemporalAdjusters.lastDayOfYear()).withHour(23).withMinute(59).withSecond(59);
    }
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/273358.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号