当前位置: 移动技术网 > IT编程>开发语言>Java > Java编写多功能万年历程序的实例分享

Java编写多功能万年历程序的实例分享

2019年07月22日  | 移动技术网IT编程  | 我要评论

 这里的万年历制作主要用到了calendar类和gregoriancalendar类,我们先来回顾一下基础知识:

基础部分

一、calendar类。
1,主要字段:
year(年份)、month(月份从0开始)、date(一月的某天)、hour(指示上午或下午的小时)、hour_f_day(指示一天中的小时。)、day_of_week (一个星期中的某天)、day_of_month(一个月中的某天)、day_of_year(一年中的某天)、day_of_week _in_month(一个月中的第几个星期)、week_of_month  (指示当前月中的星期数)、week_of_year(指示当前年中的星期数)
2,得calendar类对象。

//通过 calendar类的静态方法getinstance获得。
calendar ca = calendar.getinstance();

3,主要方法

void set(int field,int value)//field日历类的参数,比如year month date 等...
void set(int year,int month,int date)//设置年月日。
void set(int year, int month, int date, int hourofday, int minute)//设置年月日时分
void set(int year, int month, int date, int hourofday, int minute, int second)//设置年月日时分秒
void settime(date date);//使用给定的 date 设置此 calendar 的时间。
 
int get(int field)//返回给定日历字段的值。如:int month = acobj.get(calendar.month);
date gettime()//返回一个表示此 calendar 时间值的 date 对象。
long gettimeinmillis()//返回从1970.1.1 00:00:00到该日历的毫秒数。
 
void add(int field,amont);//根据日历的规则,为给定的日历字段添加或减去指定的时间量。可加减。如:caobj.add(calendar.month,1)下一个月。

二、gregoriancalendar类。
1,获得该类对象

calendar ca = new gregoriancalendar()//默认当前的时刻。
calendar ca = new gregoriancanlendar(int year,int month,int dayofmonth)//初始具有指定年月日的公历类对象。
calendar ca = new gregoriancanlendar(int year,int month,int dayofmonth,int hourofday,int minute)初始具有指定年月日的公历类对象。
calendar ca = new gregoriancanlendar(int year,int month,int dayofmonth,int hourofday,int minute,int second)//初始具有指定年月日的公历类对象。
上边的都是获得默认的语言环境,和默认的时区 对象。

2,用法
用法主要继承去父类calendar。

实例部分
三、万年历代码

package com.via.mce.monthcalendar.utils;

import java.util.calendar;
import java.util.date;
import java.util.gregoriancalendar;
import java.util.hashmap;

/**
 * 农历日历。<br>
 * 将农历从1901年到2100年之间各年、月的大小以及历年节气保存,然后基于这些数据进行计算。<br>
 * <br>
 * 新增了几个用于农历的常量属性字段,可以使用get()方法获取日历对应的值;<br>
 * 农历年、月、日还可以使用set()/add()/roll()方法设置,其他农历属性自动计算;<br>
 * 另外,还提供了getchinese(int field)方法用于获得农历的中文文字(仅适用于农历属性和星期)。<br>
 * <ul>
 * <li>chinese_year - 农历年</li>
 * <li>chinese_month - 农历月</li>
 * <li>chinese_date - 农历日</li>
 * <li>chinese_sectional_term - 当月的节气</li>
 * <li>chinese_principle_term - 当月的中气</li>
 * <li>chinese_heavenly_stem - 农历年的天干</li>
 * <li>chinese_earthly_branch - 农历年的地支</li>
 * <li>chinese_zodiac - 农历年的属相</li>
 * <li>chinese_term_or_date - 如果当天存在一个节气则指示节气,否则如果当天是初一则指示农历月,否则指示农历日</li>
 * </ul>
 * 注意:<br>
 * 由于calendar类的设定,公历月份从0起始。所有方法都遵循了这一约定。<br>
 * 但所有的农历属性从1起始。即使是在calendar提供的方法中,农历月也是从1起始的,并以负数表示闰月。<br>
 * clear()方法在某些情况下会导致农历和公历日期不对应或是不能达到预期的重置效果,应尽量避免使用。<br>
 * 使用getsimpledatestring()获得公历日期字符串时,公历月已经修正;<br>
 * 使用getsimplechinesedatestring()获得农历日期字符串时,农历闰月以*表示。<br>
 * 
 * @version 0.12 2011-9-5 <br>
 *   <blockquote>修复一个当使用农历正月日期初始化日历时陷入死循环的问题。</blockquote>
 * @version 0.11 2009-12-27 <br>
 *   <blockquote>修复了获取中文农历时未计算农历日期的问题;<br>
 *   加入一个字段chinese_term_or_date用于模仿台历的显示方式:如果当天有节气则指示节气,如果是初一指示农历月,
 *   否则指示农历日。</blockquote>
 * @version 0.10 2009-12-22
 */
public final class chinesecalendar extends gregoriancalendar {
 private static final long serialversionuid = 8l;

 /** 农历年 */
 public static final int chinese_year = 801;
 /** 农历月 */
 public static final int chinese_month = 802;
 /** 农历日 */
 public static final int chinese_date = 803;
 /** 当月的节气对应的公历日(前一个节气) */
 public static final int chinese_sectional_term = 804;
 /** 当月的中气对应的公历日(后一个节气) */
 public static final int chinese_principle_term = 805;
 /** 天干 */
 public static final int chinese_heavenly_stem = 806;
 /** 地支 */
 public static final int chinese_earthly_branch = 807;
 /** 农历年的属相(生肖) */
 public static final int chinese_zodiac = 808;
 /** 节气或者农历日 */
 public static final int chinese_term_or_date = 888;


 // add by skywang
 /** 农历节日 */
 public static final int lunar_festival = 809;
 /** 阳历节日 */
 public static final int solar_festival = 810;
 /** 节气 */
 public static final int chinese_term = 811;
 /** 月或者农历日 */
 public static final int chinese_month_or_date = 812;
 /** 节日 或 节气 或 农历日 */
 public static final int festival_or_term_or_date = 813;

 private int chineseyear;
 private int chinesemonth; // 1起始,负数表示闰月
 private int chinesedate;
 private int sectionalterm; // 当月节气的公历日
 private int principleterm; // 当月中气的公历日

 private boolean arechinesefieldscomputed; // 农历日期是否已经经过计算确认
 private boolean aresolartermscomputed; // 节气是否已经经过计算确认
 private boolean lastsetchinese; // 最后设置的是不是农历属性

 /** 使用当前时间构造一个实例。 */
 public chinesecalendar() {
  super();
 }

 /** 使用指定时间构造一个实例。 */
 public chinesecalendar(date d) {
  super.settime(d);
 }

 /** 使用指定时间构造一个实例。 */
 public chinesecalendar(calendar c) {
  this(c.gettime());
 }

 /** 使用指定公历日期构造一个实例。 */
 public chinesecalendar(int y, int m, int d) {
  super(y, m, d);
 }

 /**
  * 使用指定日期构造一个实例。
  * 
  * @param ischinese
  *   是否为农历日期
  * @param y
  * @param m
  * @param d
  */
 public chinesecalendar(boolean ischinese, int y, int m, int d) {
  if (ischinese) {
   set(chinese_year, y);
   set(chinese_month, m);
   set(chinese_date, d);
  } else {
   set(y, m, d);
  }
 }

 public void set(int field, int value) {
  computeifneed(field);

  if (ischinesefield(field)) {
   // 农历属性
   switch (field) {
   case chinese_year:
    chineseyear = value;
    break;
   case chinese_month:
    chinesemonth = value;
    break;
   case chinese_date:
    chinesedate = value;
    break;
   default:
    throw new illegalargumentexception("不支持的field设置:" + field);
   }
   lastsetchinese = true;
  } else {
   // 非农历属性
   super.set(field, value);
   lastsetchinese = false;
  }
  arefieldsset = false;
  arechinesefieldscomputed = false;
  aresolartermscomputed = false;
 }

 public int get(int field) {
  computeifneed(field);

  if (!ischinesefield(field)) {
   return super.get(field);
  }

  switch (field) {
  case chinese_year:
   return chineseyear;
  case chinese_month:
   return chinesemonth;
  case chinese_date:
   return chinesedate;
  case chinese_sectional_term:
   return sectionalterm;
  case chinese_principle_term:
   return principleterm;
  case chinese_heavenly_stem:
   return (chineseyear - 4) % 10 + 1;
  case chinese_earthly_branch:
  case chinese_zodiac:
   return (chineseyear - 4) % 12 + 1;
  case chinese_month_or_date: 
   if (get(chinese_date) == 1) {
    return chinese_month;
   } else {
    return chinese_date;
   }
  case chinese_term_or_date:
   int option;
   if (get(calendar.date) == get(chinese_sectional_term)) {
    option = chinese_sectional_term;
   } else if (get(calendar.date) == get(chinese_principle_term)) {
    option = chinese_principle_term;
   } else if (get(chinese_date) == 1) {
    option = chinese_month;
   } else {
    option = chinese_date;
   }
   return option;
  default:
   throw new illegalargumentexception("不支持的field获取:" + field);
  }
 }

 public void add(int field, int amount) {
  computeifneed(field);

  if (!ischinesefield(field)) {
   super.add(field, amount);
   lastsetchinese = false;
   arechinesefieldscomputed = false;
   aresolartermscomputed = false;
   return;
  }

  switch (field) {
  case chinese_year:
   chineseyear += amount;
   break;
  case chinese_month:
   for (int i = 0; i < amount; i++) {
    chinesemonth = nextchinesemonth(chineseyear, chinesemonth);
    if (chinesemonth == 1) {
     chineseyear++;
    }
   }
   break;
  case chinese_date:
   int maxdate = daysinchinesemonth(chineseyear, chinesemonth);
   for (int i = 0; i < amount; i++) {
    chinesedate++;
    if (chinesedate > maxdate) {
     chinesedate = 1;
     chinesemonth = nextchinesemonth(chineseyear, chinesemonth);
     if (chinesemonth == 1) {
      chineseyear++;
     }
     maxdate = daysinchinesemonth(chineseyear, chinesemonth);
    }
   }
  default:
   throw new illegalargumentexception("不支持的field:" + field);
  }

  lastsetchinese = true;
  arefieldsset = false;
  arechinesefieldscomputed = false;
  aresolartermscomputed = false;
 }

 public void roll(int field, int amount) {
  computeifneed(field);

  if (!ischinesefield(field)) {
   super.roll(field, amount);
   lastsetchinese = false;
   arechinesefieldscomputed = false;
   aresolartermscomputed = false;
   return;
  }

  switch (field) {
  case chinese_year:
   chineseyear += amount;
   break;
  case chinese_month:
   for (int i = 0; i < amount; i++) {
    chinesemonth = nextchinesemonth(chineseyear, chinesemonth);
   }
   break;
  case chinese_date:
   int maxdate = daysinchinesemonth(chineseyear, chinesemonth);
   for (int i = 0; i < amount; i++) {
    chinesedate++;
    if (chinesedate > maxdate) {
     chinesedate = 1;
    }
   }
  default:
   throw new illegalargumentexception("不支持的field:" + field);
  }

  lastsetchinese = true;
  arefieldsset = false;
  arechinesefieldscomputed = false;
  aresolartermscomputed = false;
 }

 /**
  * 获得属性的中文,可以使用的属性字段为day_of_week以及所有农历属性字段。
  * 
  * @param field
  * @return
  */
 public string getchinese(int field) {
  computeifneed(field);

  switch (field) {
  case chinese_year:
   return getchinese(chinese_heavenly_stem)
     + getchinese(chinese_earthly_branch) + "年";
  case chinese_month:
   if (chinesemonth > 0)
    return chinesemonthnames[chinesemonth] + "月";
   else
    return "闰" + chinesemonthnames[-chinesemonth] + "月";
  case chinese_date:
   return chinesedatenames[chinesedate];
  case chinese_sectional_term:
   return sectionaltermnames[get(calendar.month)];
  case chinese_principle_term:
   return principletermnames[get(calendar.month)];
  case chinese_heavenly_stem:
   return stemnames[get(field)];
  case chinese_earthly_branch:
   return branchnames[get(field)];
  case chinese_zodiac:
   return animalnames[get(field)];
  case calendar.day_of_week:
   return chineseweeknames[get(field)];
  case chinese_term_or_date:
   return getchinese(get(chinese_term_or_date));
  case lunar_festival:
   return getlunarfestival();
  case solar_festival:
   return getsolarfestival();
  case festival_or_term_or_date:
   return getfestivalortermordate();
   // todo check
  case chinese_month_or_date:
   return getchinese(get(chinese_month_or_date));
  case chinese_term:
   return getchineseterm();
  default:
   throw new illegalargumentexception("不支持的field中文获取:" + field);
  }
 }

 public string getsimplegregoriandatestring() {
  return new stringbuffer().append(get(year)).append("-")
    .append(get(month) + 1).append("-").append(get(date))
    .tostring();
 }

 public string getsimplechinesedatestring() {
  return new stringbuffer()
    .append(get(chinese_year))
    .append("-")
    .append(get(chinese_month) > 0 ? "" + get(chinese_month) : "*"
      + (-get(chinese_month))).append("-")
    .append(get(chinese_date)).tostring();
 }

 public string getchinesedatestring() {
  return new stringbuffer().append(getchinese(chinese_year))
    .append(getchinese(chinese_month))
    .append(getchinese(chinese_date)).tostring();
 }

 public string tostring() {
  stringbuffer buf = new stringbuffer();
  buf.append(getsimplegregoriandatestring()).append(" | ")
    .append(getchinese(day_of_week)).append(" | [农历]")
    .append(getchinesedatestring()).append(" ")
    .append(getchinese(chinese_zodiac)).append("年 ")
    .append(get(chinese_sectional_term)).append("日")
    .append(getchinese(chinese_sectional_term)).append(" ")
    .append(get(chinese_principle_term)).append("日")
    .append(getchinese(chinese_principle_term));
  return buf.tostring();
 }

 /**
  * 判断是不是农历属性
  * 
  * @param field
  * @return
  */
 private boolean ischinesefield(int field) {
  switch (field) {
  case chinese_year:
  case chinese_month:
  case chinese_date:
  case chinese_sectional_term:
  case chinese_principle_term:
  case chinese_heavenly_stem:
  case chinese_earthly_branch:
  case chinese_zodiac:
  case chinese_term_or_date:
  case chinese_month_or_date:
   return true;
  default:
   return false;
  }
 }

 /**
  * 判断是不是与节气有关的属性
  * 
  * @param field
  * @return
  */
 private boolean ischinesetermsfield(int field) {
  switch (field) {
  case chinese_sectional_term:
  case chinese_principle_term:
  case chinese_term_or_date:
   return true;
  default:
   return false;
  }
 }

 /**
  * 如果上一次设置的与这次将要设置或获取的属性不是同一类(农历/公历),<br>
  * 例如上一次设置的是农历而现在要设置或获取公历,<br>
  * 则需要先根据之前设置的农历日期计算出公历日期。
  * 
  * @param field
  */
 private void computeifneed(int field) {
  if (ischinesefield(field)) {
   if (!lastsetchinese && !arechinesefieldscomputed) {
    super.complete();
    computechinesefields();
    arefieldsset = true;
    arechinesefieldscomputed = true;
    aresolartermscomputed = false;
   }
   if (ischinesetermsfield(field) && !aresolartermscomputed) {
    computesolarterms();
    aresolartermscomputed = true;
   }
  } else {
   if (lastsetchinese && !arefieldsset) {
    computegregorianfields();
    super.complete();
    arefieldsset = true;
    arechinesefieldscomputed = true;
    aresolartermscomputed = false;
   }
  }
 }

 /**
  * 使用农历日期计算出公历日期
  */
 private void computegregorianfields() {
  int y = chineseyear;
  int m = chinesemonth;
  int d = chinesedate;
  arechinesefieldscomputed = true;
  arefieldsset = true;
  lastsetchinese = false;

  // 调整日期范围
  if (y < 1900)
   y = 1899;
  else if (y > 2100)
   y = 2101;

  if (m < -12)
   m = -12;
  else if (m > 12)
   m = 12;

  if (d < 1)
   d = 1;
  else if (d > 30)
   d = 30;

  int dateint = y * 10000 + math.abs(m) * 100 + d;
  if (dateint < 19001111) { // 太小
   set(1901, calendar.january, 1);
   super.complete();
  } else if (dateint > 21001201) { // 太大
   set(2100, calendar.december, 31);
   super.complete();
  } else {
   if (math.abs(m) > 12) {
    m = 12;
   }
   int days = chinesecalendar.daysinchinesemonth(y, m);
   if (days == 0) {
    m = -m;
    days = chinesecalendar.daysinchinesemonth(y, m);
   }
   if (d > days) {
    d = days;
   }
   set(y, math.abs(m) - 1, d);
   computechinesefields();

   int amount = 0;
   while (chineseyear != y || chinesemonth != m) {
    amount += daysinchinesemonth(chineseyear, chinesemonth);
    chinesemonth = nextchinesemonth(chineseyear, chinesemonth);
    if (chinesemonth == 1) {
     chineseyear++;
    }
   }
   amount += d - chinesedate;

   super.add(calendar.date, amount);
  }
  computechinesefields();
 }

 /**
  * 使用公历日期计算出农历日期
  */
 private void computechinesefields() {
  int gregorianyear = internalget(calendar.year);
  int gregorianmonth = internalget(calendar.month) + 1;
  int gregoriandate = internalget(calendar.date);

  if (gregorianyear < 1901 || gregorianyear > 2100) {
   return;
  }

  int startyear, startmonth, startdate;
  if (gregorianyear < 2000) {
   startyear = baseyear;
   startmonth = basemonth;
   startdate = basedate;
   chineseyear = basechineseyear;
   chinesemonth = basechinesemonth;
   chinesedate = basechinesedate;
  } else {
   // 第二个对应日,用以提高计算效率
   // 公历 2000 年 1 月 1 日,对应农历 4697(1999) 年 11 月 25 日
   startyear = baseyear + 99;
   startmonth = 1;
   startdate = 1;
   chineseyear = basechineseyear + 99;
   chinesemonth = 11;
   chinesedate = 25;
  }

  int daysdiff = 0;

  // 年
  for (int i = startyear; i < gregorianyear; i++) {
   if (isgregorianleapyear(i)) {
    daysdiff += 366; // leap year
   } else {
    daysdiff += 365;
   }
  }

  // 月
  for (int i = startmonth; i < gregorianmonth; i++) {
   daysdiff += daysingregorianmonth(gregorianyear, i - 1);
  }

  // 日
  daysdiff += gregoriandate - startdate;

  chinesedate += daysdiff;

  int lastdate = daysinchinesemonth(chineseyear, chinesemonth);
  while (chinesedate > lastdate) {
   chinesedate -= lastdate;
   chinesemonth = nextchinesemonth(chineseyear, chinesemonth);
   if (chinesemonth == 1) {
    chineseyear++;
   }
   lastdate = daysinchinesemonth(chineseyear, chinesemonth);
  }

 }

 /**
  * 计算节气
  */
 private void computesolarterms() {
  int gregorianyear = internalget(calendar.year);
  int gregorianmonth = internalget(calendar.month);

  if (gregorianyear < 1901 || gregorianyear > 2100) {
   return;
  }
  sectionalterm = sectionalterm(gregorianyear, gregorianmonth);
  principleterm = principleterm(gregorianyear, gregorianmonth);
 }

 /* 接下来是静态方法~ */
 /**
  * 是否为公历闰年
  * 
  * @param year
  * @return
  */
 public static boolean isgregorianleapyear(int year) {
  boolean isleap = false;
  if (year % 4 == 0) {
   isleap = true;
  }
  if (year % 100 == 0) {
   isleap = false;
  }
  if (year % 400 == 0) {
   isleap = true;
  }
  return isleap;
 }

 /**
  * 计算公历年的当月天数,公历月从0起始!
  * 
  * @param y
  * @param m
  * @return
  */
 public static int daysingregorianmonth(int y, int m) {
  int d = daysingregorianmonth[m];
  if (m == calendar.february && isgregorianleapyear(y)) {
   d++; // 公历闰年二月多一天
  }
  return d;
 }

 /**
  * 计算公历年当月的节气,公历月从0起始!
  * 
  * @param y
  * @param m
  * @return
  */
 public static int sectionalterm(int y, int m) {
  m++;
  if (y < 1901 || y > 2100) {
   return 0;
  }
  int index = 0;
  int ry = y - baseyear + 1;
  while (ry >= sectionaltermyear[m - 1][index]) {
   index++;
  }
  int term = sectionaltermmap[m - 1][4 * index + ry % 4];
  if ((ry == 121) && (m == 4)) {
   term = 5;
  }
  if ((ry == 132) && (m == 4)) {
   term = 5;
  }
  if ((ry == 194) && (m == 6)) {
   term = 6;
  }
  return term;
 }

 /**
  * 计算公历年当月的中气,公历月从0起始!
  * 
  * @param y
  * @param m
  * @return
  */
 public static int principleterm(int y, int m) {
  m++;
  if (y < 1901 || y > 2100) {
   return 0;
  }
  int index = 0;
  int ry = y - baseyear + 1;
  while (ry >= principletermyear[m - 1][index]) {
   index++;
  }
  int term = principletermmap[m - 1][4 * index + ry % 4];
  if ((ry == 171) && (m == 3)) {
   term = 21;
  }
  if ((ry == 181) && (m == 5)) {
   term = 21;
  }
  return term;
 }

 /**
  * 计算农历年的天数
  * 
  * @param y
  * @param m
  * @return
  */
 public static int daysinchinesemonth(int y, int m) {
  // 注意:闰月 m < 0
  int index = y - basechineseyear + baseindex;
  int v = 0;
  int l = 0;
  int d = 30;
  if (1 <= m && m <= 8) {
   v = chinesemonths[2 * index];
   l = m - 1;
   if (((v >> l) & 0x01) == 1) {
    d = 29;
   }
  } else if (9 <= m && m <= 12) {
   v = chinesemonths[2 * index + 1];
   l = m - 9;
   if (((v >> l) & 0x01) == 1) {
    d = 29;
   }
  } else {
   v = chinesemonths[2 * index + 1];
   v = (v >> 4) & 0x0f;
   if (v != math.abs(m)) {
    d = 0;
   } else {
    d = 29;
    for (int i = 0; i < bigleapmonthyears.length; i++) {
     if (bigleapmonthyears[i] == index) {
      d = 30;
      break;
     }
    }
   }
  }
  return d;
 }

 /**
  * 计算农历的下个月
  * 
  * @param y
  * @param m
  * @return
  */
 public static int nextchinesemonth(int y, int m) {
  int n = math.abs(m) + 1;
  if (m > 0) {
   int index = y - basechineseyear + baseindex;
   int v = chinesemonths[2 * index + 1];
   v = (v >> 4) & 0x0f;
   if (v == m) {
    n = -m;
   }
  }
  if (n == 13) {
   n = 1;
  }
  return n;
 }

 /* 日历第一天的日期 */
 private static final int baseyear = 1901;
 private static final int basemonth = 1;
 private static final int basedate = 1;
 private static final int baseindex = 0;
 private static final int basechineseyear = 1900;
 private static final int basechinesemonth = 11;
 private static final int basechinesedate = 11;

 /* 中文字符串 */
 private static final string[] chineseweeknames = { "", "星期日", "星期一", "星期二",
   "星期三", "星期四", "星期五", "星期六" };
 private static final string[] chinesemonthnames = { "", "正", "二", "三", "四",
   "五", "六", "七", "八", "九", "十", "十一", "十二" };
 private static final string[] chinesedatenames = { "", "初一", "初二", "初三",
   "初四", "初五", "初六", "初七", "初八", "初九", "初十", "十一", "十二", "十三", "十四",
   "十五", "十六", "十七", "十八", "十九", "二十", "廿一", "廿二", "廿三", "廿四", "廿五",
   "廿六", "廿七", "廿八", "廿九", "三十" };
 private static final string[] principletermnames = { "大寒", "雨水", "春分",
   "谷雨", "夏满", "夏至", "大暑", "处暑", "秋分", "霜降", "小雪", "冬至" };
 private static final string[] sectionaltermnames = { "小寒", "立春", "惊蛰",
   "清明", "立夏", "芒种", "小暑", "立秋", "白露", "寒露", "立冬", "大雪" };
 private static final string[] stemnames = { "", "甲", "乙", "丙", "丁", "戊",
   "己", "庚", "辛", "壬", "癸" };
 private static final string[] branchnames = { "", "子", "丑", "寅", "卯", "辰",
   "巳", "午", "未", "申", "酉", "戌", "亥" };
 private static final string[] animalnames = { "", "鼠", "牛", "虎", "兔", "龙",
   "蛇", "马", "羊", "猴", "鸡", "狗", "猪" };

 /* 接下来是数据压缩表~ */
 private static final int[] bigleapmonthyears = { 6, 14, 19, 25, 33, 36, 38,
   41, 44, 52, 55, 79, 117, 136, 147, 150, 155, 158, 185, 193 };
 private static final char[][] sectionaltermmap = {
   { 7, 6, 6, 6, 6, 6, 6, 6, 6, 5, 6, 6, 6, 5, 5, 6, 6, 5, 5, 5, 5, 5,
     5, 5, 5, 4, 5, 5 },
   { 5, 4, 5, 5, 5, 4, 4, 5, 5, 4, 4, 4, 4, 4, 4, 4, 4, 3, 4, 4, 4, 3,
     3, 4, 4, 3, 3, 3 },
   { 6, 6, 6, 7, 6, 6, 6, 6, 5, 6, 6, 6, 5, 5, 6, 6, 5, 5, 5, 6, 5, 5,
     5, 5, 4, 5, 5, 5, 5 },
   { 5, 5, 6, 6, 5, 5, 5, 6, 5, 5, 5, 5, 4, 5, 5, 5, 4, 4, 5, 5, 4, 4,
     4, 5, 4, 4, 4, 4, 5 },
   { 6, 6, 6, 7, 6, 6, 6, 6, 5, 6, 6, 6, 5, 5, 6, 6, 5, 5, 5, 6, 5, 5,
     5, 5, 4, 5, 5, 5, 5 },
   { 6, 6, 7, 7, 6, 6, 6, 7, 6, 6, 6, 6, 5, 6, 6, 6, 5, 5, 6, 6, 5, 5,
     5, 6, 5, 5, 5, 5, 4, 5, 5, 5, 5 },
   { 7, 8, 8, 8, 7, 7, 8, 8, 7, 7, 7, 8, 7, 7, 7, 7, 6, 7, 7, 7, 6, 6,
     7, 7, 6, 6, 6, 7, 7 },
   { 8, 8, 8, 9, 8, 8, 8, 8, 7, 8, 8, 8, 7, 7, 8, 8, 7, 7, 7, 8, 7, 7,
     7, 7, 6, 7, 7, 7, 6, 6, 7, 7, 7 },
   { 8, 8, 8, 9, 8, 8, 8, 8, 7, 8, 8, 8, 7, 7, 8, 8, 7, 7, 7, 8, 7, 7,
     7, 7, 6, 7, 7, 7, 7 },
   { 9, 9, 9, 9, 8, 9, 9, 9, 8, 8, 9, 9, 8, 8, 8, 9, 8, 8, 8, 8, 7, 8,
     8, 8, 7, 7, 8, 8, 8 },
   { 8, 8, 8, 8, 7, 8, 8, 8, 7, 7, 8, 8, 7, 7, 7, 8, 7, 7, 7, 7, 6, 7,
     7, 7, 6, 6, 7, 7, 7 },
   { 7, 8, 8, 8, 7, 7, 8, 8, 7, 7, 7, 8, 7, 7, 7, 7, 6, 7, 7, 7, 6, 6,
     7, 7, 6, 6, 6, 7, 7 } };
 private static final char[][] sectionaltermyear = {
   { 13, 49, 85, 117, 149, 185, 201, 250, 250 },
   { 13, 45, 81, 117, 149, 185, 201, 250, 250 },
   { 13, 48, 84, 112, 148, 184, 200, 201, 250 },
   { 13, 45, 76, 108, 140, 172, 200, 201, 250 },
   { 13, 44, 72, 104, 132, 168, 200, 201, 250 },
   { 5, 33, 68, 96, 124, 152, 188, 200, 201 },
   { 29, 57, 85, 120, 148, 176, 200, 201, 250 },
   { 13, 48, 76, 104, 132, 168, 196, 200, 201 },
   { 25, 60, 88, 120, 148, 184, 200, 201, 250 },
   { 16, 44, 76, 108, 144, 172, 200, 201, 250 },
   { 28, 60, 92, 124, 160, 192, 200, 201, 250 },
   { 17, 53, 85, 124, 156, 188, 200, 201, 250 } };
 private static final char[][] principletermmap = {
   { 21, 21, 21, 21, 21, 20, 21, 21, 21, 20, 20, 21, 21, 20, 20, 20,
     20, 20, 20, 20, 20, 19, 20, 20, 20, 19, 19, 20 },
   { 20, 19, 19, 20, 20, 19, 19, 19, 19, 19, 19, 19, 19, 18, 19, 19,
     19, 18, 18, 19, 19, 18, 18, 18, 18, 18, 18, 18 },
   { 21, 21, 21, 22, 21, 21, 21, 21, 20, 21, 21, 21, 20, 20, 21, 21,
     20, 20, 20, 21, 20, 20, 20, 20, 19, 20, 20, 20, 20 },
   { 20, 21, 21, 21, 20, 20, 21, 21, 20, 20, 20, 21, 20, 20, 20, 20,
     19, 20, 20, 20, 19, 19, 20, 20, 19, 19, 19, 20, 20 },
   { 21, 22, 22, 22, 21, 21, 22, 22, 21, 21, 21, 22, 21, 21, 21, 21,
     20, 21, 21, 21, 20, 20, 21, 21, 20, 20, 20, 21, 21 },
   { 22, 22, 22, 22, 21, 22, 22, 22, 21, 21, 22, 22, 21, 21, 21, 22,
     21, 21, 21, 21, 20, 21, 21, 21, 20, 20, 21, 21, 21 },
   { 23, 23, 24, 24, 23, 23, 23, 24, 23, 23, 23, 23, 22, 23, 23, 23,
     22, 22, 23, 23, 22, 22, 22, 23, 22, 22, 22, 22, 23 },
   { 23, 24, 24, 24, 23, 23, 24, 24, 23, 23, 23, 24, 23, 23, 23, 23,
     22, 23, 23, 23, 22, 22, 23, 23, 22, 22, 22, 23, 23 },
   { 23, 24, 24, 24, 23, 23, 24, 24, 23, 23, 23, 24, 23, 23, 23, 23,
     22, 23, 23, 23, 22, 22, 23, 23, 22, 22, 22, 23, 23 },
   { 24, 24, 24, 24, 23, 24, 24, 24, 23, 23, 24, 24, 23, 23, 23, 24,
     23, 23, 23, 23, 22, 23, 23, 23, 22, 22, 23, 23, 23 },
   { 23, 23, 23, 23, 22, 23, 23, 23, 22, 22, 23, 23, 22, 22, 22, 23,
     22, 22, 22, 22, 21, 22, 22, 22, 21, 21, 22, 22, 22 },
   { 22, 22, 23, 23, 22, 22, 22, 23, 22, 22, 22, 22, 21, 22, 22, 22,
     21, 21, 22, 22, 21, 21, 21, 22, 21, 21, 21, 21, 22 } };
 private static final char[][] principletermyear = {
   { 13, 45, 81, 113, 149, 185, 201 },
   { 21, 57, 93, 125, 161, 193, 201 },
   { 21, 56, 88, 120, 152, 188, 200, 201 },
   { 21, 49, 81, 116, 144, 176, 200, 201 },
   { 17, 49, 77, 112, 140, 168, 200, 201 },
   { 28, 60, 88, 116, 148, 180, 200, 201 },
   { 25, 53, 84, 112, 144, 172, 200, 201 },
   { 29, 57, 89, 120, 148, 180, 200, 201 },
   { 17, 45, 73, 108, 140, 168, 200, 201 },
   { 28, 60, 92, 124, 160, 192, 200, 201 },
   { 16, 44, 80, 112, 148, 180, 200, 201 },
   { 17, 53, 88, 120, 156, 188, 200, 201 } };

 private static final char[] daysingregorianmonth = { 31, 28, 31, 30, 31,
   30, 31, 31, 30, 31, 30, 31 };
 private static final char[] chinesemonths = { 0x00, 0x04, 0xad, 0x08, 0x5a,
   0x01, 0xd5, 0x54, 0xb4, 0x09, 0x64, 0x05, 0x59, 0x45, 0x95, 0x0a,
   0xa6, 0x04, 0x55, 0x24, 0xad, 0x08, 0x5a, 0x62, 0xda, 0x04, 0xb4,
   0x05, 0xb4, 0x55, 0x52, 0x0d, 0x94, 0x0a, 0x4a, 0x2a, 0x56, 0x02,
   0x6d, 0x71, 0x6d, 0x01, 0xda, 0x02, 0xd2, 0x52, 0xa9, 0x05, 0x49,
   0x0d, 0x2a, 0x45, 0x2b, 0x09, 0x56, 0x01, 0xb5, 0x20, 0x6d, 0x01,
   0x59, 0x69, 0xd4, 0x0a, 0xa8, 0x05, 0xa9, 0x56, 0xa5, 0x04, 0x2b,
   0x09, 0x9e, 0x38, 0xb6, 0x08, 0xec, 0x74, 0x6c, 0x05, 0xd4, 0x0a,
   0xe4, 0x6a, 0x52, 0x05, 0x95, 0x0a, 0x5a, 0x42, 0x5b, 0x04, 0xb6,
   0x04, 0xb4, 0x22, 0x6a, 0x05, 0x52, 0x75, 0xc9, 0x0a, 0x52, 0x05,
   0x35, 0x55, 0x4d, 0x0a, 0x5a, 0x02, 0x5d, 0x31, 0xb5, 0x02, 0x6a,
   0x8a, 0x68, 0x05, 0xa9, 0x0a, 0x8a, 0x6a, 0x2a, 0x05, 0x2d, 0x09,
   0xaa, 0x48, 0x5a, 0x01, 0xb5, 0x09, 0xb0, 0x39, 0x64, 0x05, 0x25,
   0x75, 0x95, 0x0a, 0x96, 0x04, 0x4d, 0x54, 0xad, 0x04, 0xda, 0x04,
   0xd4, 0x44, 0xb4, 0x05, 0x54, 0x85, 0x52, 0x0d, 0x92, 0x0a, 0x56,
   0x6a, 0x56, 0x02, 0x6d, 0x02, 0x6a, 0x41, 0xda, 0x02, 0xb2, 0xa1,
   0xa9, 0x05, 0x49, 0x0d, 0x0a, 0x6d, 0x2a, 0x09, 0x56, 0x01, 0xad,
   0x50, 0x6d, 0x01, 0xd9, 0x02, 0xd1, 0x3a, 0xa8, 0x05, 0x29, 0x85,
   0xa5, 0x0c, 0x2a, 0x09, 0x96, 0x54, 0xb6, 0x08, 0x6c, 0x09, 0x64,
   0x45, 0xd4, 0x0a, 0xa4, 0x05, 0x51, 0x25, 0x95, 0x0a, 0x2a, 0x72,
   0x5b, 0x04, 0xb6, 0x04, 0xac, 0x52, 0x6a, 0x05, 0xd2, 0x0a, 0xa2,
   0x4a, 0x4a, 0x05, 0x55, 0x94, 0x2d, 0x0a, 0x5a, 0x02, 0x75, 0x61,
   0xb5, 0x02, 0x6a, 0x03, 0x61, 0x45, 0xa9, 0x0a, 0x4a, 0x05, 0x25,
   0x25, 0x2d, 0x09, 0x9a, 0x68, 0xda, 0x08, 0xb4, 0x09, 0xa8, 0x59,
   0x54, 0x03, 0xa5, 0x0a, 0x91, 0x3a, 0x96, 0x04, 0xad, 0xb0, 0xad,
   0x04, 0xda, 0x04, 0xf4, 0x62, 0xb4, 0x05, 0x54, 0x0b, 0x44, 0x5d,
   0x52, 0x0a, 0x95, 0x04, 0x55, 0x22, 0x6d, 0x02, 0x5a, 0x71, 0xda,
   0x02, 0xaa, 0x05, 0xb2, 0x55, 0x49, 0x0b, 0x4a, 0x0a, 0x2d, 0x39,
   0x36, 0x01, 0x6d, 0x80, 0x6d, 0x01, 0xd9, 0x02, 0xe9, 0x6a, 0xa8,
   0x05, 0x29, 0x0b, 0x9a, 0x4c, 0xaa, 0x08, 0xb6, 0x08, 0xb4, 0x38,
   0x6c, 0x09, 0x54, 0x75, 0xd4, 0x0a, 0xa4, 0x05, 0x45, 0x55, 0x95,
   0x0a, 0x9a, 0x04, 0x55, 0x44, 0xb5, 0x04, 0x6a, 0x82, 0x6a, 0x05,
   0xd2, 0x0a, 0x92, 0x6a, 0x4a, 0x05, 0x55, 0x0a, 0x2a, 0x4a, 0x5a,
   0x02, 0xb5, 0x02, 0xb2, 0x31, 0x69, 0x03, 0x31, 0x73, 0xa9, 0x0a,
   0x4a, 0x05, 0x2d, 0x55, 0x2d, 0x09, 0x5a, 0x01, 0xd5, 0x48, 0xb4,
   0x09, 0x68, 0x89, 0x54, 0x0b, 0xa4, 0x0a, 0xa5, 0x6a, 0x95, 0x04,
   0xad, 0x08, 0x6a, 0x44, 0xda, 0x04, 0x74, 0x05, 0xb0, 0x25, 0x54,
   0x03 };

 private string getchineseterm() {
  if (get(calendar.date) == get(chinese_sectional_term)) {
   return sectionaltermnames[get(calendar.month)];
  } else if (get(calendar.date) == get(chinese_principle_term)) {
   return principletermnames[get(calendar.month)];
  } else
   return null;
 }
 // add by skywang
 private string getlunarfestival() {
  int day = get(chinese_date);
  int month = get(chinese_month);
  string stoday = day < 10 ? "0" + day:"" + day; 
  string smonth = month<10 ? "0" +(month):""+(month);

  return lfestival.get(smonth+stoday);  
 }
 private string getsolarfestival() {
  int day = get(calendar.date); 
  int month = get(calendar.month); 
  string stoday = day < 10 ? "0" + day:"" + day; 
  string smonth = month<10 ? "0" +(month+1):""+(month+1);

  return sfestival.get(smonth+stoday);
 }
 private string getfestivalortermordate() {
  string ret;
  if ((ret = getsolarfestival()) != null)
   return ret;
  if ((ret = getlunarfestival()) != null)
   return ret;  
  return getchinese(get(chinese_term_or_date));
 }


 //公历节日
 private static hashmap<string,string> sfestival =new hashmap<string,string>();
 // 农历介入
 private static hashmap<string,string> lfestival =new hashmap<string,string>(); 
 static {
  sfestival.put("0101","元旦"); 
  sfestival.put("0214","情人节"); 
  sfestival.put("0308","妇女节"); 
  sfestival.put("0312","植树节"); 
  sfestival.put("0401","愚人节"); 
  sfestival.put("0501","劳动节"); 
  sfestival.put("0504","青年节"); 
  sfestival.put("0601","儿童节"); 
  sfestival.put("0701","建党节"); 
  sfestival.put("0801","建军节"); 
  sfestival.put("0910","教师节"); 
  sfestival.put("1001","国庆节"); 
  sfestival.put("1031","万圣节"); 
//  sfestival.put("1112","孙中山诞辰"); 
  sfestival.put("1225","圣诞节");  

  lfestival.put("0101","春节"); 
//  lfestival.put("0102","大年初二"); 
//  lfestival.put("0103","大年初三"); 
  lfestival.put("0115","元宵节"); 
  lfestival.put("0505","端午节"); 
  lfestival.put("0707","七夕");
  lfestival.put("0815","中秋节"); 
  lfestival.put("0909","重阳节"); 
  lfestival.put("1208","腊八节"); 
//  lfestival.put("1299","除夕");
 } 
}

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网