功能:通过查表方式实现阳历和阴历的日期转换
注:该实现是参照网络上的一个用c实现的方法转换成java的
package net.walsece.date;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
public class DateParse {
private final static int MONTH_DAYS[] = {29, 30};
private final static int DAYS_MONTH[][] = {
{31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
{31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}
};
private final static int DATAS[][] = {
{23, 3, 2, 17, 1, 0, 0, 1, 0, 0, 1, 1, 0, 1, 1, 1, 0},
{41, 0, 4, 23, 1, 0, 0, 1, 0, 0, 1, 0, 1, 1, 1, 0, 1},
{30, 7, 5, 28, 1, 1, 0, 0, 1, 0, 0, 1, 0, 1, 1, 0, 1},
{49, 0, 6, 33, 1, 1, 0, 0, 1, 0, 0, 1, 0, 1, 0, 1, 1},
{38, 0, 0, 38, 1, 1, 0, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1},
{26, 6, 2, 44, 1, 1, 0, 1, 1, 0, 1, 0, 0, 1, 0, 1, 0},
{45, 0, 3, 49, 1, 0, 1, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0},
{35, 0, 4, 54, 0, 1, 0, 1, 0, 1, 1, 0, 1, 0, 1, 0, 1},
{24, 4, 5, 59, 1, 0, 1, 0, 1, 0, 1, 0, 1, 1, 0, 1, 1},
{43, 0, 0, 5, 0, 0, 1, 0, 0, 1, 0, 1, 1, 1, 0, 1, 1},
{32, 0, 1, 10, 1, 0, 0, 1, 0, 0, 1, 0, 1, 1, 0, 1, 1},
{21, 2, 2, 15, 1, 1, 0, 0, 1, 0, 0, 1, 0, 1, 0, 1, 1},
{40, 0, 3, 20, 1, 0, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1, 1},
{28, 7, 5, 26, 1, 0, 1, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1},
{47, 0, 6, 31, 0, 1, 1, 0, 1, 1, 0, 0, 1, 0, 1, 0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, 0, 0, 1, 0, 1},
{32, 0, 1, 31, 1, 1, 0, 1, 1, 0, 1, 0, 1, 0, 1, 0, 0},
{22, 3, 2, 36, 0, 1, 1, 0, 1, 0, 1, 1, 0, 1, 0, 1, 0}
};
/**
* 根据阴历取得对应的阳历日期(该日期必须在1936—2028年之间)
*
* @param date 阴历日期(字符串)
* @return 返回对应的阳历日期
* @throws Exception
*/
public static Date getGregorianCalendar(String date) {
Calendar c = getCalendar(date);
int year = c.get(Calendar.YEAR);
int month = c.get(Calendar.MONTH) + 1;
int day = c.get(Calendar.DAY_OF_MONTH);
int index = year - 1936;
int endIndex = month;
if ((DATAS[index][1] != 0) && (month > DATAS[index][1])) {
endIndex++;
}
int dayNum = 0;
for (int i = 0; i < (endIndex - 1); i++) {
dayNum += MONTH_DAYS[DATAS[index][4 + i]];
}
dayNum += day;
dayNum += DATAS[index][0];
int year_days = 365;
if (isLeapYear(year)) {
year_days = 366;
}
if (dayNum > year_days) {
year++;
dayNum -= year_days;
}
month = 1;
int dayOfMonth[] = DAYS_MONTH[0];
if (isLeapYear(year)) {
dayOfMonth = DAYS_MONTH[1];
}
int i = 0;
for (; i < 12; i++) {
dayNum -= dayOfMonth[i];
if (dayNum <= 0) {
break;
}
month++;
}
day = dayOfMonth[i] + dayNum;
return getDate(year + "-" + month + "-" + day);
}
/**
* 根据阳历取得对应的阴历日期(该日期必须在1936—2028年之间)
*
* @param date 阳历日期(字符串)
* @return 返回阴历日期
* @throws Exception
*/
public static Date getLunarCalendar(String date) {
Calendar calendar = getCalendar(date);
int year = calendar.get(Calendar.YEAR);
int month = 1;
int day;
if ((year < 1936) || (year > 2028)) {
return null;
}
int index = year - 1936;
int l_days = DATAS[index][0];
int day_year = calendar.get(Calendar.DAY_OF_YEAR);
int days;
if (day_year >= l_days) {
days = day_year - l_days;
} else {
index--;
year--;
Calendar c = getCalendar(year + "-12-31");
days = (c.get((Calendar.DAY_OF_YEAR)) + day_year) -
DATAS[index][0];
}
int i = 0;
int day_num = 0;
for (; i < 13; i++) {
day_num += MONTH_DAYS[DATAS[index][i + 4]];
if (day_num >= days) {
break;
}
month++;
}
day = MONTH_DAYS[DATAS[index][i + 4]] - (day_num - days);
if ((DATAS[index][1] != 0) && (month > DATAS[index][1])) {
month--;
}
return getDate(year + "-" + month + "-" + day);
}
private static Calendar getCalendar(String date) {
Date dd = getDate(date);
Calendar calendar = Calendar.getInstance();
calendar.setTime(dd);
return calendar;
}
private static Date getDate(String date) {
DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
Date dd = null;
try {
dd = format.parse(date);
} catch (Exception e) {
e.printStackTrace();
}
return dd;
}
private static boolean isLeapYear(int year) {
if ((year % 400) == 0) {
return true;
} else if ((year % 100) == 0) {
return false;
} else if ((year % 4) == 0) {
return true;
}
return false;
}
}
分享到:
相关推荐
java(阳历转换为农历).pdf
java 实现阳历转换农历工具类 转换日期
java 万年历(农历和阳历的互换) java 万年历(农历和阳历的互换)
一个比较综合的阴历和阳历转换的java源程序
VC++ 阳历与农历转换程序,也就是公历与阴历的相互转换,部分代码是从网上找到的,重新修正了一大部分代码,大家也可以把它当作控件来使用,因为我不初就是想想这样一个程序,奉献给大家。
(1)实现阴历与阳历的相互转换; (2)实现阳历、阴历与干支历的转换; (3)不仅包含传统阴历月的天数表,还新增历年干支月的天数表; (4)1900年历年年。
里面有5个文件:1.哪一年星期几等判断;2.公历与农历转换汇编; 3.公历与农历转换C语言实现;4.一个测试程序;5.融合的完美版
里面有5个文件:1.哪一年星期几等判断;2.公历与农历转换汇编; 3.公历与农历转换C语言实现;4.2个测试程序(vc编译器中C实现);5.融合的完美版.
PHP实现公历和农历之间的转换 一、开发环境 1、环境搭建:Windows 8+Apache 2.4.18+MySQL 5.7.11+PHP 7.1.0 。 2、文本编辑器:Sublime Text3。 二、主要技术 本实验主要使用JS类库和公历转农历的类库实现。
MySQL阳历转农历SQL实现 在mysql上通过SQL语言实现的阳历转农历, 年份跨越从1900到2049年.
公历(阳历) 农历(阴历)转换,支持时间段从 1900-2100 如果需要更长的时间段,利用 generate.htm 生成的数据即可。 支持各种编程语言 C#,java,Objective-C,php,Python,javascript(nodejs),C/C++,ruby,swift,golang 等...
信息最全的农历万年历,可以计算1901-...还可以进行农历和公历的日期对应转换,查询生辰八字。 支持自定义节日和纪念日,自定义时公历和农历是区分开的。 压缩包中包含源代码,也包含引用的自定义头文件的源C代码文件。
公历农历(阳历阴历)对照表(1900年-2100年),客户生日提醒,各种根据身份证转换阴历生日之类的业务场景必备神器。公历农历(阳历阴历)对照表(1900年-2100年),客户生日提醒,各种根据身份证转换阴历生日之类的业务场景...
vb公历农历转换.rarvb公历农历转换.rarvb公历农历转换.rarvb公历农历转换.rar
别人写的,公历和农历互相转换函数
一个关于阳历阴历的转换的Html文档 ,查看源文件可获取其实现方法!
Qt/C++实现阳历转农历功能
将阳历转换阴历: 2010年2月24日 星期三 15:10 庚寅年(虎)正月十一申时
Java 农历类,主要是自己备份用,在某大虾基础上修改的
日历转换,阳历转阴历 下载文件可以使用 package com.action.entity; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; import java.util...