
以前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);//获取一周内的某一天
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)