1、获取当前日期 Calendar cal = Calendar.getInstance(); 2、只展示今天年月日 Calendar cal = Calendar.getInstance(); cal.set(Calendar.HOUR_OF_DAY, 0); cal.set(Calendar.MINUTE, 0); cal.set(Calendar.SECOND, 0); cal.set(Calendar.MILLISECOND, 0); 3、昨天开始日期 Calendar cal = Calendar.getInstance(); cal.set(Calendar.HOUR_OF_DAY, 0); cal.set(Calendar.MINUTE, 0); cal.set(Calendar.SECOND, 0); cal.set(Calendar.MILLISECOND, 0); // DAY_OF_WEEK、DAY_OF_MONTH、DAY_OF_YEAR在add()方法中没有区别,但是在get方法中有区别,毕竟day相应和的条件不同 cal.add(Calendar.DAY_OF_WEEK, -1); 4、昨天结束日期 Calendar cal = Calendar.getInstance(); cal.set(Calendar.HOUR_OF_DAY, 0); cal.set(Calendar.MINUTE, 0); cal.set(Calendar.SECOND, 0); cal.set(Calendar.MILLISECOND, 0); 5、本周一开始日期 Calendar cal = Calendar.getInstance(); cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY); cal.set(Calendar.HOUR_OF_DAY, 0); cal.set(Calendar.MINUTE, 0); cal.set(Calendar.SECOND, 0); cal.set(Calendar.MILLISECOND, 0); 6、本周日结束日期 Calendar cal = Calendar.getInstance(); cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY); cal.set(Calendar.HOUR_OF_DAY, 0); cal.set(Calendar.MINUTE, 0); cal.set(Calendar.SECOND, 0); cal.set(Calendar.MILLISECOND, 0); cal.add(Calendar.DAY_OF_WEEK, 7); 7、距离今天XXX天的日期 Calendar cal = Calendar.getInstance(); // XXX可以是正数或者负数 cal.add(Calendar.DAY_OF_WEEK, XXX); 8、一周之后的现在 Calendar cal = Calendar.getInstance(); cal.add(Calendar.DAY_OF_WEEK, 7); 9、add()和roll()的区别 add()可以让时间跨越月份、年份等,而roll()不能跨越月份,更别提年份了
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)