转载

如何利用自定义函数把阳历转换成阴历

数据库保存的是阳历日期,有时候会遇到把阳历换成阴历的需求,例如下图把阳历转换成阴历。

如何利用自定义函数把阳历转换成阴历

 

这个问题是我在开发报表过程中遇到的一个小需求,利用的工具是FineReport8.0版本,解决思路是首先定义一个可以将阳历转为阴历的类,然后自定义FineReport函数,在run方法中获取年月日参数并调用之前的类将阳历转为阴历,最终返回给报表。

实现步骤:

1、  阳历转阴历的类

FineReport里面会提供一个现成的农历日历工具类SolarToLunar,该类中通过today(intyear, int month, int day)方法可将输入的年、月、日对应的阳历转为阴历日期,完整代码如下:



点击(此处)折叠或打开

  1. package com.fr.function;
  2. import java.text.SimpleDateFormat;
  3. import java.util.Calendar;
  4. import java.util.Date;
  5. import java.util.GregorianCalendar;
  6. import java.util.Locale;
  7. public class SolarToLunar {
  8.     final private static long[] lunarInfo = new long[] { 0x04bd8, 0x04ae0,
  9.             0x0a570, 0x054d5, 0x0d260, 0x0d950, 0x16554, 0x056a0, 0x09ad0,
  10.             0x055d2, 0x04ae0, 0x0a5b6, 0x0a4d0, 0x0d250, 0x1d255, 0x0b540,
  11.             0x0d6a0, 0x0ada2, 0x095b0, 0x14977, 0x04970, 0x0a4b0, 0x0b4b5,
  12.             0x06a50, 0x06d40, 0x1ab54, 0x02b60, 0x09570, 0x052f2, 0x04970,
  13.             0x06566, 0x0d4a0, 0x0ea50, 0x06e95, 0x05ad0, 0x02b60, 0x186e3,
  14.             0x092e0, 0x1c8d7, 0x0c950, 0x0d4a0, 0x1d8a6, 0x0b550, 0x056a0,
  15.             0x1a5b4, 0x025d0, 0x092d0, 0x0d2b2, 0x0a950, 0x0b557, 0x06ca0,
  16.             0x0b550, 0x15355, 0x04da0, 0x0a5d0, 0x14573, 0x052d0, 0x0a9a8,
  17.             0x0e950, 0x06aa0, 0x0aea6, 0x0ab50, 0x04b60, 0x0aae4, 0x0a570,
  18.             0x05260, 0x0f263, 0x0d950, 0x05b57, 0x056a0, 0x096d0, 0x04dd5,
  19.             0x04ad0, 0x0a4d0, 0x0d4d4, 0x0d250, 0x0d558, 0x0b540, 0x0b5a0,
  20.             0x195a6, 0x095b0, 0x049b0, 0x0a974, 0x0a4b0, 0x0b27a, 0x06a50,
  21.             0x06d40, 0x0af46, 0x0ab60, 0x09570, 0x04af5, 0x04970, 0x064b0,
  22.             0x074a3, 0x0ea50, 0x06b58, 0x055c0, 0x0ab60, 0x096d5, 0x092e0,
  23.             0x0c960, 0x0d954, 0x0d4a0, 0x0da50, 0x07552, 0x056a0, 0x0abb7,
  24.             0x025d0, 0x092d0, 0x0cab5, 0x0a950, 0x0b4a0, 0x0baa4, 0x0ad50,
  25.             0x055d9, 0x04ba0, 0x0a5b0, 0x15176, 0x052b0, 0x0a930, 0x07954,
  26.             0x06aa0, 0x0ad50, 0x05b52, 0x04b60, 0x0a6e6, 0x0a4e0, 0x0d260,
  27.             0x0ea65, 0x0d530, 0x05aa0, 0x076a3, 0x096d0, 0x04bd7, 0x04ad0,
  28.             0x0a4d0, 0x1d0b6, 0x0d250, 0x0d520, 0x0dd45, 0x0b5a0, 0x056d0,
  29.             0x055b2, 0x049b0, 0x0a577, 0x0a4b0, 0x0aa50, 0x1b255, 0x06d20,
  30.             0x0ada0 };

  31.     final private static int[] year20 = new int[] { 1, 4, 1, 2, 1, 2, 1, 1, 2,
  32.             1, 2, 1 };

  33.     final private static int[] year19 = new int[] { 0, 3, 0, 1, 0, 1, 0, 0, 1,
  34.             0, 1, 0 };

  35.     final private static int[] year2000 = new int[] { 0, 3, 1, 2, 1, 2, 1, 1,
  36.             2, 1, 2, 1 };

  37.     public final static String[] nStr1 = new String[] { "", "正", "二", "三", "四",
  38.             "五", "六", "七", "八", "九", "十", "十一", "十二" };

  39.     private final static String[] Gan = new String[] { "甲", "乙", "丙", "丁", "戊",
  40.             "己", "庚", "辛", "壬", "癸" };

  41.     private final static String[] Zhi = new String[] { "子", "丑", "寅", "卯", "辰",
  42.             "巳", "午", "未", "申", "酉", "戌", "亥" };

  43.     private final static String[] Animals = new String[] { "鼠", "牛", "虎", "兔",
  44.             "龙", "蛇", "马", "羊", "猴", "鸡", "狗", "猪" };

  45.     private final static String[] solarTerm = new String[] { "小寒", "大寒", "立春",
  46.             "雨水", "惊蛰", "春分", "清明", "谷雨", "立夏", "小满", "芒种", "夏至", "小暑", "大暑",
  47.             "立秋", "处暑", "白露", "秋分", "寒露", "霜降", "立冬", "小雪", "大雪", "冬至" };

  48.     private final static String[] sFtv = new String[] { "0101*元旦", "0214 情人节",
  49.             "0308 妇女节", "0312 植树节", "0315 消费者权益日", "0401 愚人节", "0501 劳动节",
  50.             "0504 青年节", "0512 护士节", "0601 儿童节", "0701 建党节", "0801 建军节",
  51.             "0808 父亲节", "0909 毛泽东逝世纪念", "0910 教师节", "0928 孔子诞辰", "1001*国庆节",
  52.             "1006 老人节", "1024 联合国日", "1112 孙中山诞辰", "1220 澳门回归", "1225 圣诞节",
  53.             "1226 毛泽东诞辰" };

  54.     private final static String[] lFtv = new String[] { "0101*农历春节",
  55.             "0115 元宵节", "0505 端午节", "0707 七夕情人节", "0815 中秋节", "0909 重阳节",
  56.             "1208 腊八节", "1224 小年", "0100*除夕" };

  57.     /**
  58.      * 传回农历 y年的总天数
  59.      *
  60.      * @param y
  61.      * @return
  62.      */
  63.     final private static int lYearDays(int y) {
  64.         int i, sum = 348;
  65.         for (i = 0x8000; i > 0x8; i >>= 1) {
  66.             if ((lunarInfo[y - 1900] & i) != 0)
  67.                 sum += 1;
  68.         }
  69.         return (sum + leapDays(y));
  70.     }

  71.     /**
  72.      * 传回农历 y年闰月的天数
  73.      *
  74.      * @param y
  75.      * @return
  76.      */
  77.     final private static int leapDays(int y) {
  78.         if (leapMonth(y) != 0) {
  79.             if ((lunarInfo[y - 1900] & 0x10000) != 0)
  80.                 return 30;
  81.             else
  82.                 return 29;
  83.         } else
  84.             return 0;
  85.     }

  86.     /**
  87.      * 传回农历 y年闰哪个月 1-12 , 没闰传回 0
  88.      *
  89.      * @param y
  90.      * @return
  91.      */
  92.     final private static int leapMonth(int y) {
  93.         return (int) (lunarInfo[y - 1900] & 0xf);
  94.     }

  95.     /**
  96.      * 传回农历 y年m月的总天数
  97.      *
  98.      * @param y
  99.      * @param m
  100.      * @return
  101.      */
  102.     final private static int monthDays(int y, int m) {
  103.         if ((lunarInfo[y - 1900] & (0x10000 >> m)) == 0)
  104.             return 29;
  105.         else
  106.             return 30;
  107.     }

  108.     /**
  109.      * 传回农历 y年的生肖
  110.      *
  111.      * @param y
  112.      * @return
  113.      */
  114.     final public static String AnimalsYear(int y) {
  115.         return Animals[(y - 4) % 12];
  116.     }

  117.     /**
  118.      * 传入 月日的offset 传回干支,0=甲子
  119.      *
  120.      * @param num
  121.      * @return
  122.      */
  123.     final private static String cyclicalm(int num) {
  124.         return (Gan[num % 10] + Zhi[num % 12]);
  125.     }

  126.     /**
  127.      * 传入 offset 传回干支, 0=甲子
  128.      *
  129.      * @param y
  130.      * @return
  131.      */
  132.     final public static String cyclical(int y) {
  133.         int num = y - 1900 + 36;
  134.         return (cyclicalm(num));
  135.     }

  136.     /**
  137.      * 传出农历.year0 .month1 .day2 .yearCyl3 .monCyl4 .dayCyl5 .isLeap6
  138.      *
  139.      * @param y
  140.      * @param m
  141.      * @return
  142.      */
  143.     final private long[] Lunar(int y, int m) {
  144.         long[] nongDate = new long[7];
  145.         int i = 0, temp = 0, leap = 0;
  146.         Date baseDate = new GregorianCalendar(1900 + 1900, 1, 31).getTime();
  147.         Date objDate = new GregorianCalendar(y + 1900, m, 1).getTime();
  148.         long offset = (objDate.getTime() - baseDate.getTime()) / 86400000L;
  149.         if (y < 2000)
  150.             offset += year19[m - 1];
  151.         if (y > 2000)
  152.             offset += year20[m - 1];
  153.         if (y == 2000)
  154.             offset += year2000[m - 1];
  155.         nongDate[5] = offset + 40;
  156.         nongDate[4] = 14;

  157.         for (i = 1900; i < 2050 && offset > 0; i++) {
  158.             temp = lYearDays(i);
  159.             offset -= temp;
  160.             nongDate[4] += 12;
  161.         }
  162.         if (offset < 0) {
  163.             offset += temp;
  164.             i--;
  165.             nongDate[4] -= 12;
  166.         }
  167.         nongDate[0] = i;
  168.         nongDate[3] = i - 1864;
  169.         leap = leapMonth(i); // 闰哪个月
  170.         nongDate[6] = 0;

  171.         for (i = 1; i < 13 && offset > 0; i++) {
  172.             // 闰月
  173.             if (leap > 0 && i == (leap + 1) && nongDate[6] == 0) {
  174.                 --i;
  175.                 nongDate[6] = 1;
  176.                 temp = leapDays((int) nongDate[0]);
  177.             } else {
  178.                 temp = monthDays((int) nongDate[0], i);
  179.             }

  180.             // 解除闰月
  181.             if (nongDate[6] == 1 && i == (leap + 1))
  182.                 nongDate[6] = 0;
  183.             offset -= temp;
  184.             if (nongDate[6] == 0)
  185.                 nongDate[4]++;
  186.         }

  187.         if (offset == 0 && leap > 0 && i == leap + 1) {
  188.             if (nongDate[6] == 1) {
  189.                 nongDate[6] = 0;
  190.             } else {
  191.                 nongDate[6] = 1;
  192.                 --i;
  193.                 --nongDate[4];
  194.             }
  195.         }
  196.         if (offset < 0) {
  197.             offset += temp;
  198.             --i;
  199.             --nongDate[4];
  200.         }
  201.         nongDate[1] = i;
  202.         nongDate[2] = offset + 1;
  203.         return nongDate;
  204.     }

  205.     /**
  206.      * 传出y年m月d日对应的农历.year0 .month1 .day2 .yearCyl3 .monCyl4 .dayCyl5 .isLeap6
  207.      *
  208.      * @param y
  209.      * @param m
  210.      * @param d
  211.      * @return
  212.      */
  213.     final public static long[] calElement(int y, int m, int d) {
  214.         long[] nongDate = new long[7];
  215.         int i = 0, temp = 0, leap = 0;
  216.         Date baseDate = new GregorianCalendar(0 + 1900, 0, 31).getTime();
  217.         Date objDate = new GregorianCalendar(y, m - 1, d).getTime();
  218.         long offset = (objDate.getTime() - baseDate.getTime()) / 86400000L;
  219.         nongDate[5] = offset + 40;
  220.         nongDate[4] = 14;

  221.         for (i = 1900; i < 2050 && offset > 0; i++) {
  222.             temp = lYearDays(i);
  223.             offset -= temp;
  224.             nongDate[4] += 12;
  225.         }
  226.         if (offset < 0) {
  227.             offset += temp;
  228.             i--;
  229.             nongDate[4] -= 12;
  230.         }
  231.         nongDate[0] = i;
  232.         nongDate[3] = i - 1864;
  233.         leap = leapMonth(i); // 闰哪个月
  234.         nongDate[6] = 0;

  235.         for (i = 1; i < 13 && offset > 0; i++) {
  236.             // 闰月
  237.             if (leap > 0 && i == (leap + 1) && nongDate[6] == 0) {
  238.                 --i;
  239.                 nongDate[6] = 1;
  240.                 temp = leapDays((int) nongDate[0]);
  241.             } else {
  242.                 temp = monthDays((int) nongDate[0], i);
  243.             }

  244.             // 解除闰月
  245.             if (nongDate[6] == 1 && i == (leap + 1))
  246.                 nongDate[6] = 0;
  247.             offset -= temp;
  248.             if (nongDate[6] == 0)
  249.                 nongDate[4]++;
  250.         }

  251.         if (offset == 0 && leap > 0 && i == leap + 1) {
  252.             if (nongDate[6] == 1) {
  253.                 nongDate[6] = 0;
  254.             } else {
  255.                 nongDate[6] = 1;
  256.                 --i;
  257.                 --nongDate[4];
  258.             }
  259.         }
  260.         if (offset < 0) {
  261.             offset += temp;
  262.             --i;
  263.             --nongDate[4];
  264.         }
  265.         nongDate[1] = i;
  266.         nongDate[2] = offset + 1;
  267.         return nongDate;
  268.     }

  269.     public final static String getChinaDate(int day) {
  270.         String a = "";
  271.         if (day == 10)
  272.             return "初十";
  273.         if (day == 20)
  274.             return "二十";
  275.         if (day == 30)
  276.             return "三十";
  277.         int two = (int) ((day) / 10);
  278.         if (two == 0)
  279.             a = "初";
  280.         if (two == 1)
  281.             a = "十";
  282.         if (two == 2)
  283.             a = "廿";
  284.         if (two == 3)
  285.             a = "三";
  286.         int one = (int) (day % 10);
  287.         switch (one) {
  288.         case 1:
  289.             a += "一";
  290.             break;
  291.         case 2:
  292.             a += "二";
  293.             break;
  294.         case 3:
  295.             a += "三";
  296.             break;
  297.         case 4:
  298.             a += "四";
  299.             break;
  300.         case 5:
  301.             a += "五";
  302.             break;
  303.         case 6:
  304.             a += "六";
  305.             break;
  306.         case 7:
  307.             a += "七";
  308.             break;
  309.         case 8:
  310.             a += "八";
  311.             break;
  312.         case 9:
  313.             a += "九";
  314.             break;
  315.         }
  316.         return a;
  317.     }

  318.     public static String today(int y, int m, int d) {
  319.         int year = y;
  320.         int month = m;
  321.         int date = d;
  322.         long[] l = calElement(year, month, date);
  323.         StringBuffer sToday = new StringBuffer();
  324.         try {

  325.             sToday.append(" 农历");
  326.             sToday.append(cyclical(year));
  327.             sToday.append('(');
  328.             sToday.append(AnimalsYear(year));
  329.             sToday.append(")年");
  330.             sToday.append(nStr1[(int) l[1]]);
  331.             sToday.append("月");
  332.             sToday.append(getChinaDate((int) (l[2])));
  333.             return sToday.toString();
  334.         } finally {
  335.             sToday = null;
  336.         }
  337.     }

  338.     private static SimpleDateFormat sdf = new SimpleDateFormat(
  339.             "yyyy年M月d日 EEEEE");

  340.     /**
  341.      * 农历日历工具使用演示
  342.      *
  343.      * @param args
  344.      */
  345.     public static void main(String[] args) {
  346.         System.out.println(today(1988, 10, 27));
  347.     }
  348. } 

2、自定义FineReport函数

自定义函数Lunar扩展于AbstractFunction,并重写run方法,获得年月日参数值,调用农历日历工具类SolarToLunar的today方法,求得农历日期并返回,代码如下:

点击(此处)折叠或打开

  1. package com.fr.function;

  2. import com.fr.script.AbstractFunction;
  3.    
  4. public class Lunar extends AbstractFunction {
  5.     public Object run(Object[] args) {
  6.         String result = "";
  7.         int y = Integer.parseInt(args[0].toString());
  8.         int m = Integer.parseInt(args[1].toString());
  9.         int d = Integer.parseInt(args[2].toString());
  10.         result = SolarToLunar.today(y, m, d);
  11.         return result;
  12.     }
  13. }

3、编译并运行

编译自定义函数

编译Lunar.java和SolarToLunar.java类生成Lunar.class和SolarToLunar.class文件拷贝至报表应用所在目录/WEB-INF/classes/com/fr/function下。

注册自定义函数

启动设计器,点击服务器|函数管理器,新增函数取名为Lunar,选择Lunar.class类,如下图:

如何利用自定义函数把阳历转换成阴历


自定义函数定义好了就可以在设计器中直接使用。

比如在单元格中写入公式=Lunar(2011,7,11),预览模板,可以看到日期2011年7月11日阳历就可以转换成阴历。

若数据库日期类型字段如图:

如何利用自定义函数把阳历转换成阴历


拖到模板设计界面,设置高级属性,在自定义显示值里面输入公式Lunar(year($),month($),day($$$)),如下图设置,则可把保存在数据库里面的阳历日期转换成阴历了。


如何利用自定义函数把阳历转换成阴历



正文到此结束
Loading...