Calendar 计算某固定日期到当前时间相差的月份
2017-07-08 10:13
369 查看
private int getMonths() {
Calendar before = Calendar.getInstance();//固定的某个日期
Calendar after = Calendar.getInstance();//当前的日期
try{
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM");//日期的展示格式
Date befDate = dateFormat.parse("2016-01");//以2016年1约为准
before .setTime(befDate);
Date aftDate = after.getTime();
after.setTime(aftDate);
int result = after.get(Calendar.MONTH) - before .get(Calendar.MONTH);
int month = (after.get(Calendar.YEAR) - before .get(Calendar.YEAR))*12;
num = result+month;
return num;
}catch (ParseException e){
e.printStackTrace();
}
return 0;
}
我们可以通过 SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM");来设定展示的时间格式,年月日时分秒,同理我们 可以获取相应的时间差!
Calendar before = Calendar.getInstance();//固定的某个日期
Calendar after = Calendar.getInstance();//当前的日期
try{
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM");//日期的展示格式
Date befDate = dateFormat.parse("2016-01");//以2016年1约为准
before .setTime(befDate);
Date aftDate = after.getTime();
after.setTime(aftDate);
int result = after.get(Calendar.MONTH) - before .get(Calendar.MONTH);
int month = (after.get(Calendar.YEAR) - before .get(Calendar.YEAR))*12;
num = result+month;
return num;
}catch (ParseException e){
e.printStackTrace();
}
return 0;
}
我们可以通过 SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM");来设定展示的时间格式,年月日时分秒,同理我们 可以获取相应的时间差!
相关文章推荐
- JAVA中的Calendar得到当前时间的年份、月份、日期
- javascript计算两个日期之间相差几个月份
- 关于时间,日期,星期,月份的算法(Java中Calendar的使用方法)
- JavaScript 遗漏知识再整理;错误处理,类型转换以及获取当前时间、年份、月份、日期;
- iOS 计算 日期 距离 当前 系统的日期 相差 多少
- 计算目标时间与当前时间的相差时
- 计算两个日期相差的天数、月份
- java Calendar 日期获取工具类,昨日开始时间,昨日结束时间,当前周周一日期,当前时间上月第一天,获取当前年第一天
- 开学时间为2009年2月18日,计算当前日期是第几周
- Calendar:计算两个日期相差的天数
- java用Calendar 计算2个时间相差 年\月\天 .
- javaScript获取当前时间、日期、月份、季度等
- C#计算某个时间距离当前日期的天数
- MYSQL计算两日期时间之间相差的天数,秒数...分钟
- php计算两个日期的相差天数,处理时间间隔
- Calendar类计算两个日期之间相差天数
- 关于时间,日期,星期,月份的算法(Java中Calendar的使用方法)
- asp.net 根据当前时间计算是否股票、期货、黄金交易日期
- Calendar:计算两个日期相差的天数
- JavaScript计算指定日期与当前日期的相差天数(未测试)