花了一整天写出的阳历和阴历互相转换的程序,希望对大家有帮助!
java吧
全部回复
仅看楼主
level 1
绝情酷哥 楼主
package com.cucu.util;/** * 返回
正确的
年月日 * @author http://www.linewell.com [email protected] * @version 1.0 */public class Date { java.util.Date d = new java.util.Date(); int date; int month; int year; int hours; int minutes; int seconds; int day; /** * @return Returns the date. */ public int getDate() { return d.getDate(); } /** * @return Returns the month. */ public int getMonth() { return d.getMonth() + 1; } /** * @return Returns the year. */ public int getYear() { int y = d.getYear(); if (y < 1900) y += 1900; return y; } /** * @return Returns the hours. */ public int getHours() { return d.getHours(); } /** * @return Returns the minutes. */ public int getMinutes() { return d.getMinutes(); } /** * @return Returns the seconds. */ public int getSeconds() { return d.getSeconds(); } /** * @return Returns the day. */ public int getDay() { return d.getDay(); }}package com.cucu.util;/** * 日期,和时间帮助类 完成阳历到阴历的转换 * @author http://www.linewell.com [email protected] * @version 1.0 */public class CalendarHelper { private int calendarData[] = new int[20]; private int madd[] = new int[12]; private Date date = new Date();//调用Date时间类 private String tgString = "甲乙丙丁戊己庚辛壬癸";//天干表 private String dzString = "子丑寅卯辰巳午未申酉戌亥";//地支表 private String numString = "一二三四五六七八九十";//中文数字 private String monString = "正二三四五六七八九十冬腊";//阴历月份 private String weekString = "日一二三四五六";//中文星期 private String sx = "鼠牛虎兔龙蛇马羊猴鸡狗猪";//属相 private int cYear;//阴历年 private int cMonth;//阴历月 private int cDay;//阴历日 private int cHour;//阴历小时 private int year = date.getYear();//阳历年 private int month = date.getMonth();//阳历月 private int day = date.getDate();//阳历日 public CalendarHelper() { init(); } public CalendarHelper(int year, int month, int day) { this.year = year; this.month = month; this.day = day; init(); } /** * @return Returns the day. */ public int getDay() { return day; } /** * @param day The day to set. */ public void setDay(int day) { this.day = day; } /** * @return Returns the month. */ public int getMonth() { return month; } /** * @param month The month to set. */ public void setMonth(int month) { this.month = month; } /** * @return Returns the year. */ public int getYear() { return year; } /** * @param year The year to set. */ public void setYear(int year) { this.year = year; } /** * @return Returns the cDay. */ public int getCDay() {
2004年12月03日 13点12分 1
level 1
绝情酷哥 楼主
return cDay; } /** * @param day The cDay to set. */ public void setCDay(int day) { cDay = day; } /** * @return Returns the cMonth. */ public int getCMonth() { return cMonth; } /** * @param month The cMonth to set. */ public void setCMonth(int month) { cMonth = month; } /** * @return Returns the cYear. */ public int getCYear() { return cYear; } /** * @param year The cYear to set. */ public void setCYear(int year) { cYear = year; }/** * 得到字节 * @param m int * @param n int * @return int 返回与结果 */ private int getBit(int m, int n) { return (m >> n) & 1; }/** * 数据初始化 */ private void init() { calendarData[0] = 0x41A95; calendarData[1] = 0xD4A; calendarData[2] = 0xDA5; calendarData[3] = 0x20B55; calendarData[4] = 0x56A; calendarData[5] = 0x7155B; calendarData[6] = 0x25D; calendarData[7] = 0x92D; calendarData[8] = 0x5192B; calendarData[9] = 0xA95; calendarData[10] = 0xB4A; calendarData[11] = 0x416AA; calendarData[12] = 0xAD5; calendarData[13] = 0x90AB5; calendarData[14] = 0x4BA; calendarData[15] = 0xA5B; calendarData[16] = 0x60A57; calendarData[17] = 0x52B; calendarData[18] = 0xA93; calendarData[19] = 0x40E95; madd[0] = 0; madd[1] = 31; madd[2] = 59; madd[3] = 90; madd[4] = 120; madd[5] = 151; madd[6] = 181; madd[7] = 212; madd[8] = 243; madd[9] = 273; madd[10] = 304; madd[11] = 334; }/** * 将阳历转成阴历的算法 * */ public void e2c() { int total, m, n, k; boolean isEnd = false; int tmp = year; total = (tmp - 2001) * 365 + (int) Math.floor((tmp - 2001) / 4) + madd[month - 1] + day - 23; if (year % 4 == 0 && month > 1) total++; for (m = 0;; m++) { k = (calendarData[m] < 0xfff) ? 11 : 12; for (n = k; n >= 0; n--) { if (total <= 29 + getBit(calendarData[m], n)) { isEnd = true; break; } total = total - 29 - getBit(calendarData[m], n); } if (isEnd) break; } cYear = 2001 + m; cMonth = k - n + 1; cDay = total; if (k == 12) { if (cMonth == Math.floor(calendarData[m] / 0x10000) + 1) cMonth = 1 - cMonth; if (cMonth > Math.floor(calendarData[m] / 0x10000) + 1) cMonth--; } cHour = (int) Math.floor((date.getHours() + 3) / 2); } /** * 得到阴历日期和时间 * @return String 阴历日期和时间 */ public String getcDateString() { String tmp = ""; tmp += tgString.charAt((cYear - 4) % 10); //天干 tmp += dzString.charAt((cYear - 4) % 12); //地支 tmp += "年("; tmp += cYear; tmp += sx.charAt((cYear - 4) % 12); tmp += ")"; if (cMonth < 1) { tmp += "闰"; tmp += monString.charAt(-cMonth - 1); } else tmp += monString.charAt(cMonth - 1);
2004年12月03日 13点12分 2
level 1
绝情酷哥 楼主
tmp += "月"; tmp += (cDay < 11) ? "初" : ((cDay < 20) ? "十" : ((cDay < 30) ? "廿" : "卅")); if (cDay % 10 != 0 || cDay == 10) tmp += numString.charAt((cDay - 1) % 10); if (cHour == 13) tmp += "夜"; tmp += dzString.charAt((cHour - 1) % 12); tmp += "时"; return tmp; } /** * 得到阳历日期和时间字符串 * @return String 阳历日期和时间字符串 */ public String getDateString() { String tmp = ""; int t1 = year; tmp += t1 + "-" + (month) + "-" + day + " " + date.getHours() + ":" + ((date.getMinutes() < 10) ? "0" : "") + date.getMinutes(); // + " 星期" + weekString.charAt(date.getDay()); return tmp; }}package com.cucu.util;import java.util.HashMap;/** * 用来供外界调用的方法,通过一个日期返回对应的阳历或者阴历. * 注意 可以通过getkey()得到任何一年的阴历日期,如果想通过阴历得到阳历日期必需 * 先通过setYear(2009)或者new Calendar(2009)设置年份 * 如果不设置年参数默认即为系统当前年 * 然后可以查到这一年所有的阳历日期如果说是润月要这样调用: * getValue("2004-0201")即要在润月前加上"-" * @author http://www.linewell.com [email protected] * @version 1.0 */public class Calendar { HashMap hashmap = new HashMap();//用来记录的键值对 CalendarHelper cp = new CalendarHelper(); Date d = new Date(); int year = d.getYear();//要进行统计的年份,默认为当前年 int monthlyDate[] = new int[] { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; public Calendar() { hashmap.clear(); add(); } public Calendar(int year) { this.year = year; hashmap.clear(); add(); } /** * @return Returns the year. */ public int getYear() { return year; } /** * @param year The year to set. */ public void setYear(int year) { this.year = year; }/** * 将数据加载到容器中 */ public void add() { if (isLeapYear(this.year)) { monthlyDate[1] = 29; } for (int i = 1; i <= 12; i++) { for (int j = 1; j <= monthlyDate[i - 1]; j++) { cp.setYear(this.year); cp.setMonth(i); cp.setDay(j); cp.e2c(); String month = ""; if (i < 10) { month = "0" + String.valueOf(i); } else { month = String.valueOf(i); } String date = ""; if (j < 10) { date = "0" + String.valueOf(j); } else { date = String.valueOf(j); } String ymd = String.valueOf(d.getYear()) + month + date; String cY = String.valueOf(cp.getCYear()); int cM_tmp = cp.getCMonth(); String cM = ""; if (cM_tmp < 10) { if (cM_tmp < 1) { cM = "-0" + String.valueOf(-cM_tmp); } else { cM = "0" + String.valueOf(cM_tmp); } } else { cM = String.valueOf(cM_tmp); } int cD_tmp = cp.getCDay(); String cD = ""; if (cD_tmp < 10) {
2004年12月03日 13点12分 3
level 1
绝情酷哥 楼主
cD = "0" + String.valueOf(cD_tmp); } else { cD = String.valueOf(cD_tmp); } put(cY + cM + cD, ymd); } } }/** * 将键值对加到HashMap里面去 * @param key String 阴历日期 * @param value String 阳历日期 */ public void put(String key, String value) { if (!hashmap.containsKey(key) && !hashmap.containsValue(value)) { hashmap.put(key, value); } }/** * 得到阴历日期 * @param value String阳历日期 * @return key String阴历日期 */ public String getKey(String value) { if (value == null || value.trim().equals("")) return ""; int y = Integer.parseInt(value.substring(0, 4)); int m = Integer.parseInt(value.substring(4, 6)); int d = Integer.parseInt(value.substring(6, 8)); cp.setYear(y); cp.setMonth(m); cp.setDay(d); cp.e2c(); String cY = String.valueOf(cp.getCYear()); int cM_tmp = cp.getCMonth(); String cM = ""; if (cM_tmp < 10) { if (cM_tmp < 1) { cM = "-0" + String.valueOf(-cM_tmp); } else { cM = "0" + String.valueOf(cM_tmp); } } else { cM = String.valueOf(cM_tmp); } int cD_tmp = cp.getCDay(); String cD = ""; if (cD_tmp < 10) { cD = "0" + String.valueOf(cD_tmp); } else { cD = String.valueOf(cD_tmp); } return cY + cM + cD; }/** * 得到阳历日期 * @param key String 阴历日期 * @return value String 阳历日期 */ public String getValue(String key) { if (key == null || key.trim().equals("")) return ""; return (String) hashmap.get(key); }/** * 判断是否为润年 * @param year 要进行判断的年 * @return boolean 是润年返回真 否则返回假 */ public boolean isLeapYear(int year) { boolean flag = false; if ((year % 4 == 0) && (year % 100 != 0) || (year % 400 == 0)) { flag = true; } return flag; } public static void main(String args[]){ Calendar c= new Calendar(); System.out.println(c.getValue("20090827")); System.out.println(c.getKey("20041010")); }}
2004年12月03日 13点12分 4
level 0
牛!
2004年12月04日 06点12分 5
level 0
辛苦了!小弟我学习中...
2004年12月04日 07点12分 6
level 0
绝情酷哥 在哪抄的啊?
2004年12月04日 10点12分 7
level 1
绝情酷哥 楼主
胡说,在哪抄的,说话要有根据好不好?见不得眼红的人,你要是能找出来我和你姓!!!
2004年12月04日 12点12分 8
level 0
其实这些代码大部分都差不多,又不是算法特别的独特所以见到类似的也不稀奇,不能说人是抄的!这种只要是会java语法的都能写,关键看你肯不肯而且各种判断好像确实又太多的例子可以拷贝——yarke_farewell
2004年12月05日 08点12分 9
level 1
绝情酷哥 楼主
实话和你们说了,以前用JavaScript写了一个,现在改成Java重新写了一次!要完成此效果把如下代码加入到区域中

CAL();
2004年12月05日 09点12分 10
level 1
什么实话不实话的,又不怀疑不是你写的
2004年12月05日 10点12分 11
level 0
不错。支持。。。。。。。。。。。我觉得比那些不服气的人强一百倍
2004年12月08日 06点12分 12
level 1
我绝对信是楼主自己写的,我们群里的老大啊,大伙有了难题就靠他了
2004年12月08日 21点12分 13
level 0
问楼主: 能不能告诉1900到2100年calendarData[]数组? 这个数组里面的值是什么意思,是如何计算的?期待你的回答!
2006年08月15日 13点08分 14
level 0
真利害,看来我还不行
2006年08月16日 09点08分 15
level 0
只要做了就很了不起了,我顶
2008年02月21日 08点02分 16
level 1

2008年02月21日 08点02分 17
level 1
牛呀!我现在正遇到这个难题.不知楼主Q号多少?想请教下楼主.我的邮箱:[email protected]
2009年03月29日 10点03分 19
level 1
佩服
2015年03月05日 05点03分 20
1