当前位置:网站首页>Get the number of days between dates, get the Chinese date, get the date of the next Monday of the date, get the working day, get the rest day
Get the number of days between dates, get the Chinese date, get the date of the next Monday of the date, get the working day, get the rest day
2022-04-23 05:01:00 【Chen junyang】
/** * Gets the number of days between dates * @param d1 * @param d2 * @return */
public int getDaysBetween(java.util.Calendar d1, java.util.Calendar d2) {
if (d1.after(d2)) {
java.util.Calendar swap = d1;
d1 = d2;
d2 = swap;
}
int days = d2.get(java.util.Calendar.DAY_OF_YEAR)
- d1.get(java.util.Calendar.DAY_OF_YEAR);
int y2 = d2.get(java.util.Calendar.YEAR);
if (d1.get(java.util.Calendar.YEAR) != y2) {
d1 = (java.util.Calendar) d1.clone();
do {
days += d1.getActualMaximum(java.util.Calendar.DAY_OF_YEAR);
d1.add(java.util.Calendar.YEAR, 1);
} while (d1.get(java.util.Calendar.YEAR) != y2);
}
return days;
}
/** * Get working days * @param d1 * @param d2 * @return */
public int getWorkingDay(java.util.Calendar d1, java.util.Calendar d2) {
int result = -1;
if (d1.after(d2)) {
java.util.Calendar swap = d1;
d1 = d2;
d2 = swap;
}
int charge_start_date = 0;// The date offset of the start date
int charge_end_date = 0;// The date offset of the end date
// The date is not in the same date
int stmp;
int etmp;
stmp = 7 - d1.get(Calendar.DAY_OF_WEEK);
etmp = 7 - d2.get(Calendar.DAY_OF_WEEK);
if (stmp != 0 && stmp != 6) {
// When the start date is Saturday and Sunday, the offset is 0
charge_start_date = stmp - 1;
}
if (etmp != 0 && etmp != 6) {
// When the end date is Saturday and Sunday, the offset is 0
charge_end_date = etmp - 1;
}
// }
result = (getDaysBetween(this.getNextMonday(d1), this.getNextMonday(d2)) / 7)
* 5 + charge_start_date - charge_end_date;
return result;
}
/** * Get Chinese date * @param date * @return */
public String getChineseWeek(Calendar date) {
final String dayNames[] = {
" Sunday ", " Monday ", " Tuesday ", " Wednesday ", " Thursday ", " Friday ", " Saturday " };
int dayOfWeek = date.get(Calendar.DAY_OF_WEEK);
return dayNames[dayOfWeek - 1];
}
/** * Get the date of the next Monday of the date * @param date * @return */
public Calendar getNextMonday(Calendar date) {
Calendar result = null;
result = date;
do {
result = (Calendar) result.clone();
result.add(Calendar.DATE, 1);
} while (result.get(Calendar.DAY_OF_WEEK) != 2);
return result;
}
/** * Get rest days * @param d1 * @param d2 * @return */
public int getHolidays(Calendar d1, Calendar d2) {
return this.getDaysBetween(d1, d2) - this.getWorkingDay(d1, d2);
}
版权声明
本文为[Chen junyang]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204220552494955.html
边栏推荐
- 深度学习笔记 —— 微调
- Mac enters MySQL terminal command
- TypeError: ‘Collection‘ object is not callable. If you meant to call the ......
- JS determines whether the numeric string contains characters
- Progress of innovation training (III)
- Day. JS common methods
- [2021] Spatio-Temporal Graph Contrastive Learning
- Painless upgrade of pixel series
- Details related to fingerprint payment
- This call when the transaction does not take effect
猜你喜欢
Windows remote connection to redis
Pixel mobile phone brick rescue tutorial
持续集成(CI)/持续交付(CD)如何彻底改变自动化测试
View analysis of scenic spots in ArcGIS
【数据库】MySQL多表查询(一)
Use the built-in function of win to transfer files between two computers in the same LAN (the speed is the same as that between local disks)
The 8 diagrams let you see the execution sequence of async / await and promise step by step
[winui3] Écrivez une copie du gestionnaire de fichiers Explorer
AQS source code reading
Teach you how to build the ruoyi system by Tencent cloud
随机推荐
redis和mysql区别
This call when the transaction does not take effect
Pixel 5 5g unlocking tutorial (including unlocking BL, installing edxposed and root)
Manually write smart pointer shared_ PTR function
leetcode——启发式搜索
MySQL slow query
DIY is an excel version of subnet calculator
Day. JS common methods
Luogu p2731 horse riding fence repair
AQS源码阅读
深度学习笔记 —— 微调
独立站运营 | FaceBook营销神器——聊天机器人ManyChat
和谐宿舍(线性dp / 区间dp)
C. Tree Infection(模拟+贪心)
Innovation training (II) task division
【数据库】MySQL单表查询
Leetcode 1547: minimum cost of cutting sticks
Perfect test of coil in wireless charging system with LCR meter
The 2021 more reading report was released, and the book consumption potential of post-95 and Post-00 rose
PHP 统计指定文件夹下文件的数量