Java中Date()getMonth()getYear()提示过时警告

Date日期类中的getMonth()与getYear()提示过时警告,这里的警告是方法过时,不建议使用

解决方法1:使用Calendar类

SimpleDateFormat ft = new SimpleDateFormat("yyyyMMdd");
Calendar calender = Calendar.getInstance();
Date date = new Date();
calender.setTime(date);
System.out.println(ft.format(calender.getTime()));
System.out.println(calender.get(Calendar.YEAR));
System.out.println(calender.get(Calendar.MONTH)+1);//获取月份是,会比数据中的月份少1,需要进行加 1 
System.out.println(calender.get(Calendar.DATE));
输出:
2023-11-02
2023
11
2

其他方法


 /***
     * 获取 日期所在 年
     *
     * */
    public static  int getYear(Date date){
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return calendar.get(Calendar.YEAR);
    }
    /***
     * 获取 日期所在 月
     *
     * */
    public static  int getMonth(Date date){

        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return  calendar.get(Calendar.MONTH) + 1;
    }
    /***
     * 获取 日期 当前天数日
     *
     * */
    public static  int getDay(Date date){

        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return  calendar.get(Calendar.DAY_OF_MONTH) ;
    }
    /***
     * 获取某一时间 所在 月份 的第一天
     *  比如 : 2020-01-20
     *     结果为: 2020-01-01 00:00:00
     * */
    public static Date getFirstDateOfMonth(Date date) {
        Calendar firstDate = Calendar.getInstance();
        firstDate.setTime(date);
        final int last = firstDate.getActualMinimum(Calendar.DAY_OF_MONTH);
        firstDate.set(Calendar.DAY_OF_MONTH, last);
        //当前日期 的 月份的 第一天)
        return  firstDate.getTime();
    }
    /***
     * 获取某一时间 所在 月份 的最后一天
     *  比如 : 2020-01-20
     *     结果为: 2020-01-31 00:00:00
     * */
    public static Date getEndDayOfMonth(Date date){

        Calendar lastDateMonth = Calendar.getInstance();
        lastDateMonth.setTime(date );
        final int lastDay = lastDateMonth.getActualMaximum(Calendar.DAY_OF_MONTH);
        lastDateMonth.set(Calendar.DAY_OF_MONTH, lastDay);
        //当前日期 的月份的最后一天
        return  lastDateMonth.getTime();
    }

    /***
     * 获取某一时间 所在 月份 下个月的第一天
     *  比如 : 2020-01-20
     *     结果为:2020-02-01 00:00:00
     * */
    public static Date getFirstDayOfNext(Date date){

        Calendar nextMonthFirst = Calendar.getInstance();
        nextMonthFirst.setTime(date);
        nextMonthFirst.set(Calendar.DAY_OF_MONTH, 1);
        nextMonthFirst.add(Calendar.MONTH, 1);
        return  nextMonthFirst.getTime();
    }

解决方法2:使用Date类截取

SimpleDateFormat year = new SimpleDateFormat("yyyy");
SimpleDateFormat mm = new SimpleDateFormat("MM");
Calendar calender = Calendar.getInstance();
Date date = new Date();
System.out.println(yyyy.format(date));
System.out.println(mm.format(date));

输出:
2023-06-02
2023
06
如果用月份的话,直接就是06, 并不是6;不用再加格式 s%
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
JavaDate类提供了一些常用的方法来处理日期和时间。下面是一些常见的Date类的方法: 1. `Date()`:无参构造方法,创建一个表示当前时间的Date对象。 2. `Date(long date)`:根据给定的毫秒数创建一个Date对象,表示从1970年1月1日00:00:00 GMT开始的指定时间。 3. `after(Date when)`:判断当前Date对象是否在参数Date对象之后。 4. `before(Date when)`:判断当前Date对象是否在参数Date对象之前。 5. `getTime()`:返回自1970年1月1日00:00:00 GMT以来的毫秒数。 6. `toString()`:将Date对象转换为字符串表示形式。 7. `equals(Object obj)`:判断当前Date对象是否与参数对象相等。 8. `compareTo(Date anotherDate)`:比较当前Date对象与参数Date对象的顺序。 9. `setTime(long time)`:设置Date对象表示的时间。 10. `getYear()`:返回当前Date对象表示的年份(从1900年开始计算,所以需要加上1900)。 11. `getMonth()`:返回当前Date对象表示的月份(从0开始,0表示一月)。 12. `getDate()`:返回当前Date对象表示的日期(月份的某一天)。 13. `getDay()`:返回当前Date对象表示的星期几(0表示星期日,1表示星期一,以此类推)。 14. `getHours()`:返回当前Date对象表示的小时数。 15. `getMinutes()`:返回当前Date对象表示的分钟数。 16. `getSeconds()`:返回当前Date对象表示的秒数。 这些方法可以帮助你在Java处理日期和时间相关的操作。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值