Calendar使用

Calendar使用,第1张

Calendar使用
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()不能跨越月份,更别提年份了

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

原文地址: http://outofmemory.cn/zaji/5695315.html

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

发表评论

登录后才能评论

评论列表(0条)

保存