1 Star 0 Fork 0

quietor / calendar-scheduler

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
CalculateWeekdayWithJulian.java 1.89 KB
一键复制 编辑 原始数据 按行查看 历史
quietor 提交于 2018-06-27 07:24 . 调整星期计算代码
package com.quietor.calendar.week;
import com.quietor.calendar.constants.CalendarMonth;
import com.quietor.calendar.exception.CalendarException;
import com.quietor.calendar.tool.CalendarTool;
/**
* @author Quietor
* @create 2018-06-17 19:34
* @desc 根据已知星期的日期计算指定日志的星期
* 利用儒略日(Julian Day,JD)进行星期的推算
* 儒略日是一种不记年,不记月,只记日的历法,是完全基于天数的
* 计算公式
* days = int(1461*(y+4716)/4) + int(153*(m+1)/5) + d + c -1524.5
* y表示为年份
* m月份,如果m小于或等于2,则m修正为m+12,同时年份修正为y-1
* c的取值公式:c = 2 - int(y/100) + int(y/400)
* int表示取整,舍去小数部分
**/
public class CalculateWeekdayWithJulian {
/**
* 根据儒略日公式进行计算儒略日
* @param year
* @param month
* @param day
* @return
* @throws CalendarException
*/
public static int calculateDaysWithJulian(int year, CalendarMonth month, int day) throws CalendarException {
CalendarTool.checkParam(year, month, day);
int monthVal = month.getSort();
if(month.compare(CalendarMonth.FEBRUARY) <= 0) {
monthVal = month.getSort() + 12;
year = year - 1;
}
//计算 c 值
int c = 2 - Integer.valueOf(year/100) + Integer.valueOf(year/400);
day += 0.5000115740;//因为儒略日的开始是从中午12点开始的,所以需要加0.5天, 0.0000115740天=1秒
return (int) (Integer.valueOf(1461 * (year + 4716) / 4) + Integer.valueOf(153 * (monthVal + 1)/5) + day + c - 1514.5);
}
public static void main(String[] args) throws CalendarException {
int end = calculateDaysWithJulian(2005, CalendarMonth.MAY, 31);
int start = calculateDaysWithJulian(1977, CalendarMonth.MARCH, 27);
System.out.println(end - start);
}
}
1
https://gitee.com/quietor/calendar-scheduler.git
git@gitee.com:quietor/calendar-scheduler.git
quietor
calendar-scheduler
calendar-scheduler
master

搜索帮助