Calendar日历类显示年、季度、月、天
来源:互联网 发布:itunes不能下载软件 编辑:程序博客网 时间:2024/06/11 09:54
使用Calendar获取时间分量
1.生成实例
Calendar cal1 = Calendar.getInstance();
2.设置 时间 按照:年 月 日 时 分 秒
cal1.set(cal1.get(Calendar.YEAR),cal1.get(Calendar.MONDAY), cal1.get(Calendar.DAY_OF_MONTH), 0, 0,0);
3.时间获得相差的天数
// 得微秒级时间差
long val = calendarEnd.getTimeInMillis() - calendarBegin.getTimeInMillis();
// 换算后得到天数
long day = val / (1000 * 60 * 60 * 24);
3.例子
Calendar cal1 = Calendar.getInstance();
Calendar cal2 = Calendar.getInstance();
if("day".equals("timeZone")){ //时间分量 当天
cal1.set(cal1.get(Calendar.YEAR),cal1.get(Calendar.MONDAY), cal1.get(Calendar.DAY_OF_MONTH), 0, 0,0);
cal2.set(cal2.get(Calendar.YEAR),cal2.get(Calendar.MONDAY), cal2.get(Calendar.DAY_OF_MONTH), 0, 0,0);
cal2.set(Calendar.HOUR_OF_DAY, 24);
}
if("month".equals("timeZone")){ //时间分量 当月
cal1.set(cal1.get(Calendar.YEAR),cal1.get(Calendar.MONDAY), cal1.get(Calendar.DAY_OF_MONTH), 0, 0,0);
cal1.set(Calendar.DAY_OF_MONTH,cal1.getActualMinimum(Calendar.DAY_OF_MONTH));
cal2.set(cal2.get(Calendar.YEAR),cal2.get(Calendar.MONDAY), cal2.get(Calendar.DAY_OF_MONTH), 0, 0,0);
cal2.set(Calendar.DAY_OF_MONTH,cal2.getActualMaximum(Calendar.DAY_OF_MONTH));
cal2.set(Calendar.HOUR_OF_DAY, 24);
}
if("Quarterly".equals("timeZone")){ //当前 季度
int currentMonth = cal1.get(Calendar.MONTH) + 1;
if (currentMonth >= 1 && currentMonth <= 3) {
//一季度
cal1.set(cal1.get(Calendar.YEAR),0, 1, 0, 0,0);
cal2.set(cal1.get(Calendar.YEAR),3, 0, 0, 0,0);
} else if (currentMonth >= 4 && currentMonth <= 6) {
//二季度
cal1.set(cal1.get(Calendar.YEAR),3, 1, 0, 0,0);
cal2.set(cal1.get(Calendar.YEAR),6, 0, 0, 0,0);
} else if (currentMonth >= 7 && currentMonth <= 9) {
//三季度
cal1.set(cal1.get(Calendar.YEAR),6, 1, 0, 0,0);
cal2.set(cal1.get(Calendar.YEAR),9, 0, 0, 0,0);
} else if (currentMonth >= 10 && currentMonth <= 12) {
//四季度
cal1.set(cal1.get(Calendar.YEAR),9, 1, 0, 0,0);
cal2.set(cal1.get(Calendar.YEAR),12, 0, 0, 0,0);
}
}
if("year".equals("timeZone")){ //当前年份
cal1.set(cal1.get(Calendar.YEAR),0, 1, 0, 0,0);
cal2.set(cal2.get(Calendar.YEAR)+1,0, 0, 0, 0,0);
}
0 0
- Calendar日历类显示年、季度、月、天
- Calendar--显示某个月
- 日历类Calendar
- Java日历类Calendar
- Calendar 日历类
- Calendar类日历
- Calendar 日历类
- java Calendar日历类
- java Calendar类(日历)
- 日历类 Calendar
- Calendar日历类
- Calendar日历类,Date类
- java的Calendar(日历)类
- java的Calendar(日历)类
- Calendar 日历类的使用方法
- Calendar类得到当前日历
- Calendar类实现打印日历
- java 之 Calendar 日历类
- Java计算ln(N!)
- Android 系统第三方应用系统修改权限及在应用上层显示权限默认打开
- JavaScript 数组乱序
- error during connect: Get http://%2F%2F.%2Fpipe%2Fdocker_engine/v1.28/version: open //./pipe/docker_
- C++第6次上机实验:【项目2】数组合并
- Calendar日历类显示年、季度、月、天
- 中断中判断状态标志位
- 01 Matrix
- 网页截屏生产pdf或者截取某个标签生产pdf(打印某个标签)
- Spring官网下载最新的spring-framework-4.3.8.RELEASE-dist.zip
- innodb引擎的redo log日志的原理
- doom3 源代码评测 1
- JNI学习积累之一 ---- 常用函数大全
- Maven/Gradle 在Spring Boot中配置log4j