当前位置:网站首页>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
边栏推荐
- 深度学习笔记 —— 微调
- Introduction to load balancing
- #define 定义常量和宏,指针和结构体
- POI export message list (including pictures)
- Innovation training (II) task division
- 多线程基本概念(并发与并行、线程与进程)和入门案例
- MySQL uses or to query SQL, and SQL execution is very slow
- Wine (COM) - basic concept
- 深度学习笔记 —— 数据增广
- Raspberry pie + opencv + opencv -- face detection ------- environment construction
猜你喜欢
DIY 一个 Excel 版的子网计算器
Field injection is not recommended using @ Autowired
What are the redis data types
Deep learning notes - data expansion
Wechat payment function
[database] MySQL multi table query (I)
View, modify and delete [database] table
Thoughts on a small program
#define 定义常量和宏,指针和结构体
拼了!两所A级大学,六所B级大学,纷纷撤销软件工程硕士点!
随机推荐
Progress of innovation training (III)
SCP command details
MySQL time function query
深度学习笔记 —— 物体检测和数据集 + 锚框
[WinUI3]编写一个仿Explorer文件管理器
QPushbutton 槽函数被多次触发
Windows remote connection to redis
Docker installation and mysql5 7 installation
Unity C e-learning (IV)
L2-011 play binary tree (build tree + BFS)
Learning Android from scratch -- baseactivity and activitycollector
The vscode ipynb file does not have code highlighting and code completion solutions
【数据库】MySQL基本操作(基操~)
C# List字段排序含有数字和字符
Customize the navigation bar at the top of wechat applet (adaptive wechat capsule button, flex layout)
Innovation training (IX) integration
信息学奥赛一本通 1955:【11NOIP普及组】瑞士轮 | OpenJudge 4.1 4363:瑞士轮 | 洛谷 P1309 [NOIP2011 普及组] 瑞士轮
Opencv + clion face recognition + face model training
Teach you how to build the ruoyi system by Tencent cloud
MySQL memo (for your own query)