package com.java;
import java.util.Calendar;
public class LunarCalendar {
public static void main(String[] args) {
Solar s = new Solar();
s.getDate(2020, 9);
String t = MutualConversion.solarToLundar(2009, 10, 12);
String[] str = t.split("-");
Lunar la = new Lunar(Integer.parseInt(str[0]), Integer
.parseInt(str[1]), Integer.parseInt(str[2]));
System.out.println("\n\n阳历2009-10-12日对应的农历日期为:"+la.toString(1)+" "+la.toWeek());
String t1 = MutualConversion.lundarToSolar(2019, 1, 1);
String[] str1 = t1.split("-");
Solar s1 = new Solar(Integer.parseInt(str1[0]), Integer
.parseInt(str1[1]), Integer.parseInt(str1[2]));
System.out.println("\n农历2019-1-1日对应的阳历日期为:"+s1.toString()+" "+s1.toWeek());
}
}
// 自定义日历类,其作用是实现阳历和农历日期相互转换的功能
class MutualConversion {
// 阵列storeLunarMonth存储在每月一天的资料,每年从1901年到2100年的农历,农历只能是29或30天,
// 每月表达12(或13)的二进制位在一年内,这是30 1中的相应位置的形式天,否则,29天
private static final int[]storeLunarMonth = {
0x4ae0,0xa570,0x5268,0xd260,0xd950,0x6aa8,
0x56a0,0x9ad0,0x4ae8,0x4ae0,//1910
0xa4d8,0xa4d0,0xd250,0xd548,0xb550,0x56a0,
0x96d0,0x95b0,0x49b8,0x49b0,//1920
0xa4b0,0xb258,0x6a50,0x6d40,0xada8,
0x2b60,0x9570,0x4978,0x4970,0x64b0, //1930
0xd4a0,0xea50,0x6d48,0x5ad0,0x2b60,
0x9370,0x92e0,0xc968,0xc950,0xd4a0, //1940
0xda50,0xb550,0x56a0,0xaad8,0x25d0,
0x92d0,0xc958,0xa950,0xb4a8,0x6ca0, //1950
0xb550,0x55a8,0x4da0,0xa5b0,0x52b8,
0x52b0,0xa950,0xe950,0x6aa0,0xad50, //1960
0xab50,0x4b60,0xa570,0xa570,
0x5260,0xe930,0xd950,0x5aa8,0x56a0,0x96d0, //1970
0x4ae8,0x4ad0,0xa4d0,0xd268,0xd250,
0xd528,0xb540,0xb6a0,0x96d0,0x95b0, //1980
0x49b0,0xa4b8,0xa4b0,0xb258,0x6a50,
0x6d40,0xada0,0xab60,0x9370,0x4978, // 1990
0x4970,0x64b0,0x6a50,0xea50,0x6b28,
0x5ac0,0xab60,0x9368,0x92e0,0xc960, //2000
0xd4a8,0xd4a0,0xda50,0x5aa8,0x56a0,
0xaad8,0x25d0,0x92d0,0xc958,0xa950, //2010
0xb4a0,0xb550,0xb550,0x55a8,0x4ba0
,0xa5b0,0x52b8,0x52b0,0xa930,0x74a8, //2020
0x6aa0,0xad50,0x4da8,0x4b60,0x9570,
0xa4e0,0xd260,0xe930,0xd530,0x5aa0, //2030
0x6b50,0x96d0,0x4ae8,0x4ad0,0xa4d0,
0xd258,0xd250,0xd520,0xdaa0,0xb5a0, //2040
0x56d0,0x4ad8,0x49b0,0xa4b8,0xa4b0,
0xaa50,0xb528,0x6d20,0xada0,0x55b0 //2050
};
// 阵列storeLunarLeapMonth存储的是农历1901年至2050年闰月的信息,0表示该年没有闰月,每个字符元素表示的存储两年。
// 例如0x50该字符存储的是2009和2010年,0x表示八进制,5表示2009年闰5月,0表示2010没有闰月
private static final char[] storeLunarLeapMonth = { 0x00, 0x50, 0x04, 0x00,
0x20, // 1910
0x60, 0x05, 0x00, 0x20, 0x70, // 1920
0x05, 0x00, 0x40, 0x02, 0x06, // 1930
0x00, 0x50, 0x03, 0x07, 0x00, // 1940
0x60, 0x04, 0x00, 0x20, 0x70, // 1950
0x05, 0x00, 0x30, 0x80, 0x06, // 1960
0x00, 0x40, 0x03, 0x07, 0x00, // 1970
0x50, 0x04, 0x08, 0x00, 0x60, // 1980
0x04, 0x0a, 0x00, 0x60, 0x05, // 1990
0x00, 0x30, 0x80, 0x05, 0x00, // 2000
0x40, 0x02, 0x07, 0x00, 0x50, // 2010
0x04, 0x09, 0x00, 0x60, 0x04, // 2020
0x00, 0x20, 0x60, 0x05, 0x00, // 2030
0x30, 0xb0, 0x06, 0x00, 0x50, // 2040
0x02, 0x07, 0x00, 0x50, 0x03 // 2050
};
// 用矩阵存领存储从1901~2050年每一年的阳历和农历的偏移天数
private static final char[] offseOfDays = { 49, 38, 28, 46, 34, 24, 43, 32,
21, 40, // 1910
29, 48, 36, 25, 44, 34, 22, 41, 31, 50, // 1920
38, 27, 46, 35, 23, 43, 32, 22, 40, 29, // 1930
47, 36, 25, 44, 34, 23, 41, 30, 49, 38, // 1940
26, 45, 35, 24, 43, 32, 21, 40, 28, 47, // 1950
36, 26, 44, 33, 23, 42, 30, 48, 38, 27, // 1960
45, 35, 24, 43, 32, 20, 39, 29, 47, 36, // 1970
26, 45, 33, 22, 41, 30, 48, 37, 27, 46, // 1980
35, 24, 43, 32, 50, 39, 28, 47, 36, 26, // 1990
45, 34, 22, 40, 30, 49, 37, 27, 46, 35, // 2000
23, 42, 31, 21, 39, 28, 48, 37, 25, 44, // 2010
33, 23, 41, 31, 50, 39, 28, 47, 35, 24, // 2020
42, 30, 21, 40, 28, 47, 36, 25, 43, 33, // 2030
22, 41, 30, 49, 37, 26, 44, 33, 23, 42, // 2040
31, 21, 40, 29, 47, 36, 25, 44, 32, 22, // 2050
};
static boolean isLeapYearOfSolar(int year) {// 判断是否有闰年
return ((year % 4 == 0) && (year % 100 != 0) || year % 400 == 0);
}
// 获取阳历中每个月的天数
static int getSolarMonthOfDays(int year, int month) {
if ((month == 1) || (month == 3) || (month == 5) || (month == 7)
|| (month == 8) || (month == 10) || (month == 12))
return 31;
else if ((month == 4) || (month == 6) || (month == 9) || (month == 11))
return 30;
else if (month == 2) {
if (isLeapYearOfSolar(year))
return 29;
else
return 28;
} else
return 0;
}
// 获取新年的偏移天
static int L_getOffsetOfDays(int year, int month, int day) {
int days = 0;
for (int i = 1; i < month; i++) {
days += getSolarMonthOfDays(year, i);
}
days += day - 1;
return days;
}
static int L_getLeapMonth(int year) {
char month = storeLunarLeapMonth[(year - 1901) / 2];
if (year % 2 == 0)
return (month & 0x0f);
else
return (month & 0xf0) >> 4;
}
// 获取当前年月的农历月
static int L_getMonthDays(int year, int month) {
int leapMonth = L_getLeapMonth(year);
if ((month > 12) && (month - 12 != leapMonth) || (month < 0)) {
System.out.println("输入的月份有错误");
return -1;
}
if (month - 12 == leapMonth) {
if ((storeLunarMonth[year - 1901] & (0x8000 >> leapMonth)) == 0)
return 29;
else
return 30;
}
if ((leapMonth > 0) && (month > leapMonth))
month++;
if ((storeLunarMonth[year - 1901] & (0x8000 >> (month - 1))) == 0)
return 29;
else
return 30;
}
// 获取当前年份的农历年
static int getLunarYear(int year) {
int iYearDays = 0;
int leapMonth = L_getLeapMonth(year);
for (int i = 1; i < 13; i++)
iYearDays += L_getMonthDays(year, i);
if (leapMonth > 0)
iYearDays += L_getMonthDays(year, leapMonth + 12);
return iYearDays;
}
static int getOffsetOfDays(int year, int month, int day) {
int days = 0;
int leapMonth = L_getLeapMonth(year);
if ((leapMonth > 0) && (leapMonth == month - 12)) {
month = leapMonth;
days += L_getMonthDays(year, month);
}
for (int i = 1; i < month; i++) {
days += L_getMonthDays(year, i);
if (i == leapMonth)
days += L_getMonthDays(year, leapMonth + 12);
}
days += day - 1;
return days;
}
// 阳历转换成农历
static String solarToLundar(int year, int month, int day) {
int L_day, L_month, L_year;
int days = L_getOffsetOfDays(year, month, day);
int leapMonth = L_getLeapMonth(year);
if (days < offseOfDays[year - 1901]) {
L_year = year - 1;
days = offseOfDays[year - 1901] - days;
L_day = days;
for (L_month = 12; days > L_getMonthDays(L_year, L_month); L_month--) {
L_day = days;
days -= L_getMonthDays(L_year, L_month);
}
if (0 == L_day)
L_day = 1;
else
L_day = L_getMonthDays(L_year, L_month) - days + 1;
} else {
L_year = year;
days -= offseOfDays[year - 1901];
L_day = days + 1;
for (L_month = 1; days >= 0; L_month++) {
L_day = days + 1;
days -= L_getMonthDays(L_year, L_month);
if ((leapMonth == L_month) && (days > 0)) {
L_day = days;
days -= L_getMonthDays(L_year, L_month + 12);
if (days <= 0) {
L_month += 12 + 1;
break;
}
}
}
L_month--;
}
return "" + L_year + "-" + L_month + "-" + L_day;
}
// 农历转换成阳历
static String lundarToSolar(int year, int month, int day) {
int S_year, S_month, S_day;
int days = getOffsetOfDays(year, month, day) + offseOfDays[year - 1901];
int iYearDays = isLeapYearOfSolar(year) ? 366 : 365;
if (days >= iYearDays) {
S_year = year + 1;
days -= iYearDays;
} else {
S_year = year;
}
S_day = days + 1;
for (S_month = 1; days >= 0; S_month++) {
S_day = days + 1;
days -= getSolarMonthOfDays(S_ye
java 万年历(农历和阳历的互换)
4星 · 超过85%的资源 需积分: 31 25 浏览量
2010-06-21
15:17:24
上传
评论 3
收藏 63KB ZIP 举报
pingping45
- 粉丝: 0
- 资源: 43
最新资源
- 基于Vue+Echarts实现风力发电机中传感器的数据展示监控可视化系统+源代码+文档说明(高分课程设计)
- 基于单片机的风力发电机转速控制源码
- 基于C++实现的风力发电气动平衡监测系统+源代码+测量数据(高分课程设计)
- 毕业设计- 基于STM32F103C8T6 单片机,物联网技术的太阳能发电装置+源代码+文档说明+架构图+界面截图
- 基于 LSTM(长短期记忆)(即改进的循环神经网络)预测风力发电厂中风力涡轮机产生的功率+源代码+文档说明
- 基于stm32f103+空心杯电机+oled按键+运动算法
- 《CKA/CKAD应试指南/从docker到kubernetes 完全攻略》学习笔记 第1章docker基础(1.1-1.4)
- 基于python实现的水下压缩空气储能互补系统建模仿真与经济效益分析+源代码+论文
- 华中科技大学-自然语言处理实验,Bi-LSTM+CRF的中文分词框架,并且利用基于深度学习的方法进行中文命名实体识别++源码报告
- 基于动态罚函数的铁路车流分配与径路优化模型python源码
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
- 1
- 2
前往页