java获取年月日

java获取年月日,第1张

java获取年月日

以前getYear之类的,不过早已被淘汰,以下是目前常用的方法
获取现在的年月日

    Calendar cal = Calendar.getInstance();
    Date date=new Date;//现在的日期
    cal.setTime(date);
    Integer year=cal.get(Calendar.YEAR);//获取年
    Integer month = cal.get(Calendar.MONTH)+1;//获取月(月份从0开始,如果按照中国的习惯,需要加一)
    Integer day_moneth=cal.get(Calendar.DAY_OF_MONTH);//获取日(月中的某一天)
    Integer day_week=cal.get(Calendar.DAY_OF_WEEK);//获取一周内的某一天

获取指定日期的年月日

    String str_date="2020-01-03";

    //将字符串转化为日期格式
    Date date = new SimpleDateFormat("yyyy-MM-dd").parse(str_date);
    Calendar cal = Calendar.getInstance();
    cal.setTime(date);
    Integer year=cal.get(Calendar.YEAR);//获取年
    Integer month = cal.get(Calendar.MONTH)+1;//获取月(月份从0开始,如果按照中国的习惯,需要加一)
    Integer day_moneth=cal.get(Calendar.DAY_OF_MONTH);//获取日(月中的某一天)
    Integer day_week=cal.get(Calendar.DAY_OF_WEEK);//获取一周内的某一天

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/zaji/5710000.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-12-17
下一篇2022-12-17

发表评论

登录后才能评论

评论列表(0条)

    保存