[Java] 农历和阳历相互转换源码 →→→→→进入此内容的聊天室

来自 , 2020-12-01, 写在 Java, 查看 107 次.
URL http://www.code666.cn/view/10a5ab2d
  1. /**
  2.  * 农历和阳历相互转换源码
  3.  *
  4.  */
  5. public class CalendarConverter {
  6.         public static void main(String[] args) {
  7.                 // 调用农历日期转换阳历日期方法
  8.                 System.out.println(ChineseCalendar.sCalendarLundarToSolar(2008, 1, 1));
  9.         }
  10. }
  11.  
  12. // 自定义日历类
  13. class ChineseCalendar {
  14.  
  15.         // Array lIntLunarDay is stored in the monthly day information in every year
  16.         // from 1901 to 2100 of the lunar calendar,
  17.         // The lunar calendar can only be 29 or 30 days every month, express with
  18.         // 12(or 13) pieces of binary bit in one year,
  19.         // it is 30 days for 1 form in the corresponding location , otherwise it is
  20.         // 29 days
  21.         private static final int[] iLunarMonthDaysTable = {
  22.                         0x4ae0,
  23.                         0xa570,
  24.                         0x5268,
  25.                         0xd260,
  26.                         0xd950,
  27.                         0x6aa8,
  28.                         0x56a0,
  29.                         0x9ad0,
  30.                         0x4ae8,
  31.                         0x4ae0, // 1910
  32.                         0xa4d8,
  33.                         0xa4d0,
  34.                         0xd250,
  35.                         0xd548,
  36.                         0xb550,
  37.                         0x56a0,
  38.                         0x96d0,
  39.                         0x95b0,
  40.                         0x49b8,
  41.                         0x49b0, // 1920
  42.                         0xa4b0,
  43.                         0xb258,
  44.                         0x6a50,
  45.                         0x6d40,
  46.                         0xada8,
  47.                         0x2b60,
  48.                         0x9570,
  49.                         0x4978,
  50.                         0x4970,
  51.                         0x64b0, // 1930
  52.                         0xd4a0,
  53.                         0xea50,
  54.                         0x6d48,
  55.                         0x5ad0,
  56.                         0x2b60,
  57.                         0x9370,
  58.                         0x92e0,
  59.                         0xc968,
  60.                         0xc950,
  61.                         0xd4a0, // 1940
  62.                         0xda50,
  63.                         0xb550,
  64.                         0x56a0,
  65.                         0xaad8,
  66.                         0x25d0,
  67.                         0x92d0,
  68.                         0xc958,
  69.                         0xa950,
  70.                         0xb4a8,
  71.                         0x6ca0, // 1950
  72.                         0xb550,
  73.                         0x55a8,
  74.                         0x4da0,
  75.                         0xa5b0,
  76.                         0x52b8,
  77.                         0x52b0,
  78.                         0xa950,
  79.                         0xe950,
  80.                         0x6aa0,
  81.                         0xad50, // 1960
  82.                         0xab50,
  83.                         0x4b60,
  84.                         0xa570,
  85.                         0xa570,
  86.                         0x5260,
  87.                         0xe930,
  88.                         0xd950,
  89.                         0x5aa8,
  90.                         0x56a0,
  91.                         0x96d0, // 1970
  92.                         0x4ae8, 0x4ad0,
  93.                         0xa4d0,
  94.                         0xd268,
  95.                         0xd250,
  96.                         0xd528,
  97.                         0xb540,
  98.                         0xb6a0,
  99.                         0x96d0,
  100.                         0x95b0, // 1980
  101.                         0x49b0, 0xa4b8, 0xa4b0,
  102.                         0xb258,
  103.                         0x6a50,
  104.                         0x6d40,
  105.                         0xada0,
  106.                         0xab60,
  107.                         0x9370,
  108.                         0x4978, // 1990
  109.                         0x4970, 0x64b0, 0x6a50, 0xea50,
  110.                         0x6b28,
  111.                         0x5ac0,
  112.                         0xab60,
  113.                         0x9368,
  114.                         0x92e0,
  115.                         0xc960, // 2000
  116.                         0xd4a8, 0xd4a0, 0xda50, 0x5aa8, 0x56a0,
  117.                         0xaad8,
  118.                         0x25d0,
  119.                         0x92d0,
  120.                         0xc958,
  121.                         0xa950, // 2010
  122.                         0xb4a0, 0xb550, 0xb550, 0x55a8, 0x4ba0, 0xa5b0,
  123.                         0x52b8,
  124.                         0x52b0,
  125.                         0xa930,
  126.                         0x74a8, // 2020
  127.                         0x6aa0, 0xad50, 0x4da8, 0x4b60, 0x9570, 0xa4e0, 0xd260,
  128.                         0xe930,
  129.                         0xd530,
  130.                         0x5aa0, // 2030
  131.                         0x6b50, 0x96d0, 0x4ae8, 0x4ad0, 0xa4d0, 0xd258, 0xd250, 0xd520,
  132.                         0xdaa0,
  133.                         0xb5a0, // 2040
  134.                         0x56d0, 0x4ad8, 0x49b0, 0xa4b8, 0xa4b0, 0xaa50, 0xb528, 0x6d20,
  135.                         0xada0, 0x55b0 // 2050
  136.         };
  137.  
  138.         // Array iLunarLeapMonthTable preserves the lunar calendar leap month from
  139.         // 1901 to 2050,
  140.         // if it is 0 express not to have , every byte was stored for two years
  141.         private static final char[] iLunarLeapMonthTable = { 0x00, 0x50, 0x04,
  142.                         0x00, 0x20, // 1910
  143.                         0x60, 0x05, 0x00, 0x20, 0x70, // 1920
  144.                         0x05, 0x00, 0x40, 0x02, 0x06, // 1930
  145.                         0x00, 0x50, 0x03, 0x07, 0x00, // 1940
  146.                         0x60, 0x04, 0x00, 0x20, 0x70, // 1950
  147.                         0x05, 0x00, 0x30, 0x80, 0x06, // 1960
  148.                         0x00, 0x40, 0x03, 0x07, 0x00, // 1970
  149.                         0x50, 0x04, 0x08, 0x00, 0x60, // 1980
  150.                         0x04, 0x0a, 0x00, 0x60, 0x05, // 1990
  151.                         0x00, 0x30, 0x80, 0x05, 0x00, // 2000
  152.                         0x40, 0x02, 0x07, 0x00, 0x50, // 2010
  153.                         0x04, 0x09, 0x00, 0x60, 0x04, // 2020
  154.                         0x00, 0x20, 0x60, 0x05, 0x00, // 2030
  155.                         0x30, 0xb0, 0x06, 0x00, 0x50, // 2040
  156.                         0x02, 0x07, 0x00, 0x50, 0x03 // 2050
  157.         };
  158.  
  159.         // Array iSolarLunarTable stored the offset days
  160.         // in New Year of solar calendar and lunar calendar from 1901 to 2050;
  161.         private static final char[] iSolarLunarOffsetTable = { 49, 38, 28, 46, 34,
  162.                         24, 43, 32, 21, 40, // 1910
  163.                         29, 48, 36, 25, 44, 34, 22, 41, 31, 50, // 1920
  164.                         38, 27, 46, 35, 23, 43, 32, 22, 40, 29, // 1930
  165.                         47, 36, 25, 44, 34, 23, 41, 30, 49, 38, // 1940
  166.                         26, 45, 35, 24, 43, 32, 21, 40, 28, 47, // 1950
  167.                         36, 26, 44, 33, 23, 42, 30, 48, 38, 27, // 1960
  168.                         45, 35, 24, 43, 32, 20, 39, 29, 47, 36, // 1970
  169.                         26, 45, 33, 22, 41, 30, 48, 37, 27, 46, // 1980
  170.                         35, 24, 43, 32, 50, 39, 28, 47, 36, 26, // 1990
  171.                         45, 34, 22, 40, 30, 49, 37, 27, 46, 35, // 2000
  172.                         23, 42, 31, 21, 39, 28, 48, 37, 25, 44, // 2010
  173.                         33, 23, 41, 31, 50, 39, 28, 47, 35, 24, // 2020
  174.                         42, 30, 21, 40, 28, 47, 36, 25, 43, 33, // 2030
  175.                         22, 41, 30, 49, 37, 26, 44, 33, 23, 42, // 2040
  176.                         31, 21, 40, 29, 47, 36, 25, 44, 32, 22, // 2050
  177.         };
  178.  
  179.         static boolean bIsSolarLeapYear(int iYear) {
  180.                 return ((iYear % 4 == 0) && (iYear % 100 != 0) || iYear % 400 == 0);
  181.         }
  182.  
  183.         // The days in the month of solar calendar
  184.         static int iGetSYearMonthDays(int iYear, int iMonth) {
  185.                 if ((iMonth == 1) || (iMonth == 3) || (iMonth == 5) || (iMonth == 7)
  186.                                 || (iMonth == 8) || (iMonth == 10) || (iMonth == 12))
  187.                         return 31;
  188.                 else if ((iMonth == 4) || (iMonth == 6) || (iMonth == 9)
  189.                                 || (iMonth == 11))
  190.                         return 30;
  191.                 else if (iMonth == 2) {
  192.                         if (bIsSolarLeapYear(iYear))
  193.                                 return 29;
  194.                         else
  195.                                 return 28;
  196.                 } else
  197.                         return 0;
  198.         }
  199.  
  200.         // The offset days from New Year and the day when point out in solar
  201.         // calendar
  202.         static int iGetSNewYearOffsetDays(int iYear, int iMonth, int iDay) {
  203.                 int iOffsetDays = 0;
  204.  
  205.                 for (int i = 1; i < iMonth; i++) {
  206.                         iOffsetDays += iGetSYearMonthDays(iYear, i);
  207.                 }
  208.                 iOffsetDays += iDay - 1;
  209.  
  210.                 return iOffsetDays;
  211.         }
  212.  
  213.         static int iGetLLeapMonth(int iYear) {
  214.                 char iMonth = iLunarLeapMonthTable[(iYear - 1901) / 2];
  215.  
  216.                 if (iYear % 2 == 0)
  217.                         return (iMonth & 0x0f);
  218.                 else
  219.                         return (iMonth & 0xf0) >> 4;
  220.         }
  221.  
  222.         static int iGetLMonthDays(int iYear, int iMonth) {
  223.                 int iLeapMonth = iGetLLeapMonth(iYear);
  224.                 if ((iMonth > 12) && (iMonth - 12 != iLeapMonth) || (iMonth < 0)) {
  225.                         System.out
  226.                                         .println("Wrong month, ^_^ , i think you are want a -1, go to death!");
  227.                         return -1;
  228.                 }
  229.                 if (iMonth - 12 == iLeapMonth) {
  230.                         if ((iLunarMonthDaysTable[iYear - 1901] & (0x8000 >> iLeapMonth)) == 0)
  231.                                 return 29;
  232.                         else
  233.                                 return 30;
  234.                 }
  235.                 if ((iLeapMonth > 0) && (iMonth > iLeapMonth))
  236.                         iMonth++;
  237.                 if ((iLunarMonthDaysTable[iYear - 1901] & (0x8000 >> (iMonth - 1))) == 0)
  238.                         return 29;
  239.                 else
  240.                         return 30;
  241.         }
  242.  
  243.         // Days in this year of lunar calendar
  244.         static int iGetLYearDays(int iYear) {
  245.                 int iYearDays = 0;
  246.                 int iLeapMonth = iGetLLeapMonth(iYear);
  247.  
  248.                 for (int i = 1; i < 13; i++)
  249.                         iYearDays += iGetLMonthDays(iYear, i);
  250.                 if (iLeapMonth > 0)
  251.                         iYearDays += iGetLMonthDays(iYear, iLeapMonth + 12);
  252.                 return iYearDays;
  253.         }
  254.  
  255.         static int iGetLNewYearOffsetDays(int iYear, int iMonth, int iDay) {
  256.                 int iOffsetDays = 0;
  257.                 int iLeapMonth = iGetLLeapMonth(iYear);
  258.  
  259.                 if ((iLeapMonth > 0) && (iLeapMonth == iMonth - 12)) {
  260.                         iMonth = iLeapMonth;
  261.                         iOffsetDays += iGetLMonthDays(iYear, iMonth);
  262.                 }
  263.  
  264.                 for (int i = 1; i < iMonth; i++) {
  265.                         iOffsetDays += iGetLMonthDays(iYear, i);
  266.                         if (i == iLeapMonth)
  267.                                 iOffsetDays += iGetLMonthDays(iYear, iLeapMonth + 12);
  268.                 }
  269.                 iOffsetDays += iDay - 1;
  270.  
  271.                 return iOffsetDays;
  272.         }
  273.  
  274.         // The solar calendar is turned into the lunar calendar
  275.         static String sCalendarSolarToLundar(int iYear, int iMonth, int iDay) {
  276.                 int iLDay, iLMonth, iLYear;
  277.                 int iOffsetDays = iGetSNewYearOffsetDays(iYear, iMonth, iDay);
  278.                 int iLeapMonth = iGetLLeapMonth(iYear);
  279.  
  280.                 if (iOffsetDays < iSolarLunarOffsetTable[iYear - 1901]) {
  281.                         iLYear = iYear - 1;
  282.                         iOffsetDays = iSolarLunarOffsetTable[iYear - 1901] - iOffsetDays;
  283.                         iLDay = iOffsetDays;
  284.  
  285.                         for (iLMonth = 12; iOffsetDays > iGetLMonthDays(iLYear, iLMonth); iLMonth--) {
  286.                                 iLDay = iOffsetDays;
  287.                                 iOffsetDays -= iGetLMonthDays(iLYear, iLMonth);
  288.                         }
  289.                         if (0 == iLDay)
  290.                                 iLDay = 1;
  291.                         else
  292.                                 iLDay = iGetLMonthDays(iLYear, iLMonth) - iOffsetDays + 1;
  293.                 } else {
  294.                         iLYear = iYear;
  295.                         iOffsetDays -= iSolarLunarOffsetTable[iYear - 1901];
  296.                         iLDay = iOffsetDays + 1;
  297.  
  298.                         for (iLMonth = 1; iOffsetDays >= 0; iLMonth++) {
  299.                                 iLDay = iOffsetDays + 1;
  300.                                 iOffsetDays -= iGetLMonthDays(iLYear, iLMonth);
  301.                                 if ((iLeapMonth == iLMonth) && (iOffsetDays > 0)) {
  302.                                         iLDay = iOffsetDays;
  303.                                         iOffsetDays -= iGetLMonthDays(iLYear, iLMonth + 12);
  304.                                         if (iOffsetDays <= 0) {
  305.                                                 iLMonth += 12 + 1;
  306.                                                 break;
  307.                                         }
  308.                                 }
  309.                         }
  310.                         iLMonth--;
  311.                 }
  312.                 return "" + iLYear + (iLMonth > 9 ? "" + iLMonth : "0" + iLMonth)
  313.                                 + (iLDay > 9 ? "" + iLDay : "0" + iLDay);
  314.         }
  315.  
  316.         // The lunar calendar is turned into the Solar calendar
  317.         static String sCalendarLundarToSolar(int iYear, int iMonth, int iDay) {
  318.                 int iSYear, iSMonth, iSDay;
  319.                 int iOffsetDays = iGetLNewYearOffsetDays(iYear, iMonth, iDay)
  320.                                 + iSolarLunarOffsetTable[iYear - 1901];
  321.                 int iYearDays = bIsSolarLeapYear(iYear) ? 366 : 365;
  322.  
  323.                 if (iOffsetDays >= iYearDays) {
  324.                         iSYear = iYear + 1;
  325.                         iOffsetDays -= iYearDays;
  326.                 } else {
  327.                         iSYear = iYear;
  328.                 }
  329.                 iSDay = iOffsetDays + 1;
  330.                 for (iSMonth = 1; iOffsetDays >= 0; iSMonth++) {
  331.                         iSDay = iOffsetDays + 1;
  332.                         iOffsetDays -= iGetSYearMonthDays(iSYear, iSMonth);
  333.                 }
  334.                 iSMonth--;
  335.  
  336.                 return "" + iSYear + (iSMonth > 9 ? iSMonth + "" : "0" + iSMonth)
  337.                                 + (iSDay > 9 ? iSDay + "" : "0" + iSDay);
  338.         }
  339. }
  340.  
  341. // 自定义星期类
  342. class Week {
  343.         int iWeek;
  344.  
  345.         private String sWeek[] = { "Sunday", "Monday", "Tuesday", "Wednesday",
  346.                         "Thursday", "Friday", "Saturday" };
  347.  
  348.         public Week() {
  349.                 iWeek = 0;
  350.         }
  351.  
  352.         public Week(int w) {
  353.                 if ((w > 6) || (w < 0)) {
  354.                         System.out.println("Week out of range, I think you want Sunday");
  355.                         this.iWeek = 0;
  356.                 } else
  357.                         this.iWeek = w;
  358.         }
  359.  
  360.         public String toString() {
  361.                 return sWeek[iWeek];
  362.         }
  363. }
  364.  
  365. // 自定义日期类
  366. class MyDate {
  367.  
  368.         public int iYear;
  369.  
  370.         public int iMonth;
  371.  
  372.         public int iDay;
  373.  
  374.         private static int checkYear(int iYear) {
  375.                 if ((iYear > 1901) && (iYear < 2050))
  376.                         return iYear;
  377.                 else {
  378.                         System.out.println("The Year out of range, I think you want 1981");
  379.                         return 1981;
  380.                 }
  381.         }
  382.  
  383.         public MyDate(int iYear, int iMonth, int iDay) {
  384.                 this.iYear = checkYear(iYear);
  385.                 this.iMonth = iMonth;
  386.                 this.iDay = iDay;
  387.         }
  388.  
  389.         public MyDate(int iYear, int iMonth) {
  390.                 this.iYear = checkYear(iYear);
  391.                 this.iMonth = iMonth;
  392.                 this.iDay = 1;
  393.         }
  394.  
  395.         public MyDate(int iYear) {
  396.                 this.iYear = checkYear(iYear);
  397.                 this.iMonth = 1;
  398.                 this.iDay = 1;
  399.         }
  400.  
  401.         public MyDate() {
  402.                 this.iYear = 1981;
  403.                 this.iMonth = 1;
  404.                 this.iDay = 1;
  405.         }
  406.  
  407.         public String toString() {
  408.                 return "" + this.iYear
  409.                                 + (this.iMonth > 9 ? "" + this.iMonth : "0" + this.iMonth)
  410.                                 + (this.iDay > 9 ? "" + this.iDay : "0" + this.iDay);
  411.         }
  412.  
  413.         public boolean equals(MyDate md) {
  414.                 return ((md.iDay == this.iDay) && (md.iMonth == this.iMonth) && (md.iYear == this.iYear));
  415.         }
  416. }
  417.  
  418. // 阳历日期类,继承自定义日期
  419. class SolarDate extends MyDate {
  420.  
  421.         private static int checkMonth(int iMonth) {
  422.                 if (iMonth > 12) {
  423.                         System.out.println("Month out of range, I think you want 12 ");
  424.                         return 12;
  425.                 } else if (iMonth < 1) {
  426.                         System.out.println("Month out of range, I think you want 1 ");
  427.                         return 1;
  428.                 } else
  429.                         return iMonth;
  430.         }
  431.  
  432.         private static int checkDay(int iYear, int iMonth, int iDay) {
  433.                 int iMonthDays = ChineseCalendar.iGetSYearMonthDays(iYear, iMonth);
  434.  
  435.                 if (iDay > iMonthDays) {
  436.                         System.out.println("Day out of range, I think you want "
  437.                                         + iMonthDays + " ");
  438.                         return iMonthDays;
  439.                 } else if (iDay < 1) {
  440.                         System.out.println("Day out of range, I think you want 1 ");
  441.                         return 1;
  442.                 } else
  443.                         return iDay;
  444.         }
  445.  
  446.         public SolarDate(int iYear, int iMonth, int iDay) {
  447.                 super(iYear);
  448.                 this.iMonth = checkMonth(iMonth);
  449.                 this.iDay = checkDay(this.iYear, this.iMonth, iDay);
  450.         }
  451.  
  452.         public SolarDate(int iYear, int iMonth) {
  453.                 super(iYear);
  454.                 this.iMonth = checkMonth(iMonth);
  455.         }
  456.  
  457.         public SolarDate(int iYear) {
  458.                 super(iYear);
  459.         }
  460.  
  461.         public SolarDate() {
  462.                 super();
  463.         }
  464.  
  465.         public String toString() {
  466.                 return "" + this.iYear
  467.                                 + (this.iMonth > 9 ? "-" + this.iMonth : "-0" + this.iMonth)
  468.                                 + (this.iDay > 9 ? "-" + this.iDay : "-0" + this.iDay);
  469.         }
  470.  
  471.         public Week toWeek() {
  472.                 int iOffsetDays = 0;
  473.                 for (int i = 1901; i < iYear; i++) {
  474.                         if (ChineseCalendar.bIsSolarLeapYear(i))
  475.                                 iOffsetDays += 366;
  476.                         else
  477.                                 iOffsetDays += 365;
  478.                 }
  479.  
  480.                 iOffsetDays += ChineseCalendar.iGetSNewYearOffsetDays(iYear, iMonth,
  481.                                 iDay);
  482.                 return new Week((iOffsetDays + 2) % 7);
  483.         }
  484.  
  485.         public LunarDate toLunarDate() {
  486.                 int iYear, iMonth, iDay, iDate;
  487.                 LunarDate ld;
  488.                 iDate = Integer.parseInt(ChineseCalendar.sCalendarSolarToLundar(
  489.                                 this.iYear, this.iMonth, this.iDay));
  490.                 iYear = iDate / 10000;
  491.                 iMonth = iDate % 10000 / 100;
  492.                 iDay = iDate % 100;
  493.                 ld = new LunarDate(iYear, iMonth, iDay);
  494.                 return ld;
  495.         }
  496. }
  497.  
  498. // 阴历日期类,继承自定义日期类
  499. class LunarDate extends MyDate {
  500.  
  501.         private String sChineseNum[] = { "零", "一", "二", "三", "四", "五", "六", "七",
  502.                         "八", "九", "十" };
  503.  
  504.         private static int checkMonth(int iYear, int iMonth) {
  505.                 if ((iMonth > 12)
  506.                                 && (iMonth == ChineseCalendar.iGetLLeapMonth(iYear) + 12)) {
  507.                         return iMonth;
  508.                 } else if (iMonth > 12) {
  509.                         System.out.println("Month out of range, I think you want 12 ");
  510.                         return 12;
  511.                 } else if (iMonth < 1) {
  512.                         System.out.println("Month out of range, I think you want 1 ");
  513.                         return 1;
  514.                 } else
  515.                         return iMonth;
  516.         }
  517.  
  518.         private static int checkDay(int iYear, int iMonth, int iDay) {
  519.                 int iMonthDays = ChineseCalendar.iGetLMonthDays(iYear, iMonth);
  520.  
  521.                 if (iDay > iMonthDays) {
  522.                         System.out.println("Day out of range, I think you want "
  523.                                         + iMonthDays + " ");
  524.                         return iMonthDays;
  525.                 } else if (iDay < 1) {
  526.                         System.out.println("Day out of range, I think you want 1 ");
  527.                         return 1;
  528.                 } else
  529.                         return iDay;
  530.         }
  531.  
  532.         public LunarDate(int iYear, int iMonth, int iDay) {
  533.                 super(iYear);
  534.                 this.iMonth = checkMonth(this.iYear, iMonth);
  535.                 this.iDay = checkDay(this.iYear, this.iMonth, iDay);
  536.         }
  537.  
  538.         public LunarDate(int iYear, int iMonth) {
  539.                 super(iYear);
  540.                 this.iMonth = checkMonth(this.iYear, iMonth);
  541.         }
  542.  
  543.         public LunarDate(int iYear) {
  544.                 super(iYear);
  545.         }
  546.  
  547.         public LunarDate() {
  548.                 super();
  549.         }
  550.  
  551.         public String toString() {
  552.                 String sCalendar = "农历";
  553.  
  554.                 sCalendar += sChineseNum[iYear / 1000]
  555.                                 + sChineseNum[iYear % 1000 / 100]
  556.                                 + sChineseNum[iYear % 100 / 10] + sChineseNum[iYear % 10] + "("
  557.                                 + toChineseEra() + ")年";
  558.                 if (iMonth > 12) {
  559.                         iMonth -= 12;
  560.                         sCalendar += "闰";
  561.                 }
  562.                 if (iMonth == 12)
  563.                         sCalendar += "腊月";
  564.                 else if (iMonth == 11)
  565.                         sCalendar += "冬月";
  566.                 else if (iMonth == 1)
  567.                         sCalendar += "正月";
  568.                 else
  569.                         sCalendar += sChineseNum[iMonth] + "月";
  570.                 if (iDay > 29)
  571.                         sCalendar += "三十";
  572.                 else if (iDay > 20)
  573.                         sCalendar += "二十" + sChineseNum[iDay % 20];
  574.                 else if (iDay == 20)
  575.                         sCalendar += "二十";
  576.                 else if (iDay > 10)
  577.                         sCalendar += "十" + sChineseNum[iDay % 10];
  578.                 else
  579.                         sCalendar += "初" + sChineseNum[iDay];
  580.  
  581.                 return sCalendar;
  582.         }
  583.  
  584.         public CnWeek toWeek() {
  585.                 int iOffsetDays = 0;
  586.                 for (int i = 1901; i < iYear; i++)
  587.                         iOffsetDays += ChineseCalendar.iGetLYearDays(i);
  588.  
  589.                 iOffsetDays += ChineseCalendar.iGetLNewYearOffsetDays(iYear, iMonth,
  590.                                 iDay);
  591.                 return new CnWeek((iOffsetDays + 2) % 7);
  592.         }
  593.  
  594.         public ChineseEra toChineseEra() {
  595.                 return new ChineseEra(iYear);
  596.         }
  597.  
  598.         public SolarDate toSolarDate() {
  599.                 int iYear, iMonth, iDay, iDate;
  600.                 SolarDate sd;
  601.                 iDate = Integer.parseInt(ChineseCalendar.sCalendarLundarToSolar(
  602.                                 this.iYear, this.iMonth, this.iDay));
  603.                 iYear = iDate / 10000;
  604.                 iMonth = iDate % 10000 / 100;
  605.                 iDay = iDate % 100;
  606.                 sd = new SolarDate(iYear, iMonth, iDay);
  607.                 return sd;
  608.         }
  609. }
  610.  
  611. class CnWeek extends Week {
  612.  
  613.         private String sCnWeek[] = { "日", "一", "二", "三", "四", "五", "六" };
  614.  
  615.         public CnWeek() {
  616.                 super();
  617.         }
  618.  
  619.         public CnWeek(int iWeek) {
  620.                 super(iWeek);
  621.         }
  622.  
  623.         public String toString() {
  624.                 return "星期" + sCnWeek[this.iWeek];
  625.         }
  626. }
  627.  
  628. class ChineseEra {
  629.         int iYear;
  630.  
  631.         String[] sHeavenlyStems = { "甲", "乙", "丙", "丁", "戊", "己", "庚", "辛", "壬",
  632.                         "癸" };
  633.  
  634.         String[] sEarthlyBranches = { "子", "丑", "寅", "卯", "辰", "巳", "午", "未", "申",
  635.                         "酉", "戌", "亥" };
  636.  
  637.         public ChineseEra() {
  638.                 int iYear = 1981;
  639.         }
  640.  
  641.         public ChineseEra(int iYear) {
  642.                 if ((iYear < 2050) && (iYear > 1901))
  643.                         this.iYear = iYear;
  644.                 else
  645.                         this.iYear = 1981;
  646.         }
  647.  
  648.         public String toString() {
  649.                 int temp;
  650.                 temp = Math.abs(iYear - 1924);
  651.                 return sHeavenlyStems[temp % 10] + sEarthlyBranches[temp % 12];
  652.         }
  653. }

回复 "农历和阳历相互转换源码"

这儿你可以回复上面这条便签

captcha