Perpetual calendar (lunar calendar conversion)
Package Com.java;import Java.util.calendar;public class Lunarcalendar {public static void main (string[] args) {Solar s = n EW 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 ("The date of the lunar calendar 2009-10-12 corresponds to:" +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" Lunar calendar date for 2019-1-1: "+s1.tostring () +" "+s1.toweek ());}} Custom Calendar class, which functions as a function of converting the Gregorian and lunar dates to each other. class Mutualconversion {//array storelunarmonth stored on the day of the month, each year from 1901 to 2100 the lunar calendar can only be 29 or 30 days ,//Monthly expression 12 (or 13) of the bits within a year, which is 30 1 in the form of the corresponding location of the day, otherwise, 29 days private static Final int[]storelunarmonth = {0x4ae0,0xa570,0x5268, 0xd260,0xd950,0x6aa8,0x56a0,0x9ad0,0x4ae8,0x4ae0,//19100xa4d8,0xa4d0,0xd250,0xd548,0xb550,0X56a0,0x96d0,0x95b0,0x49b8,0x49b0,//19200xa4b0,0xb258,0x6a50,0x6d40,0xada8,0x2b60,0x9570,0x4978,0x4970,0x64b0, 19300xd4a0,0xea50,0x6d48,0x5ad0,0x2b60,0x9370,0x92e0,0xc968,0xc950,0xd4a0,//19400xda50,0xb550,0x56a0,0xaad8, 0X25D0,0X92D0,0XC958,0XA950,0XB4A8,0X6CA0,//19500XB550,0X55A8,0X4DA0,0XA5B0,0X52B8,0X52B0,0XA950,0XE950,0X6AA0 , 0XAD50,//19600xab50,0x4b60,0xa570,0xa570,0x5260,0xe930,0xd950,0x5aa8,0x56a0,0x96d0,//19700x4ae8,0x4ad0,0xa4d0 , 0xd268,0xd250,0xd528,0xb540,0xb6a0,0x96d0,0x95b0,//19800x49b0,0xa4b8,0xa4b0,0xb258,0x6a50,0x6d40,0xada0, 0xab60,0x9370,0x4978,//19900x4970,0x64b0,0x6a50,0xea50,0x6b28,0x5ac0,0xab60,0x9368,0x92e0,0xc960,//20000XD4A8, 0xd4a0,0xda50,0x5aa8,0x56a0,0xaad8,0x25d0,0x92d0,0xc958,0xa950,//20100xb4a0,0xb550,0xb550,0x55a8,0x4ba0,0xa5b0 , 0x52b8,0x52b0,0xa930,0x74a8,//20200X6AA0,0XAD50,0X4DA8,0X4B60,0X9570,0XA4E0,0XD260,0XE930,0XD530,0X5AA0,// 20300x6b50,0x96d0,0x4ae8,0x4ad0,0xa4d0,0xd258,0xd250,0xd520,0xdaa0,0xb5a0,//20400x56d0,0x4ad8,0x49b0,0xa4b8, 0xa4b0,The 0xaa50,0xb528,0x6d20,0xada0,0x55b0//2050};//array Storelunarleapmonth stores information about the lunar calendar from 1901 to 2050, 0 means that there is no leap month in the year, Each character element represents the storage for two years. For example 0x50 this character is stored in 2009 and 2010, 0x for octal, 5 for 2009 Leap May, 0 for 2010 no leap month for private static final char[] Storelunarleapmonth = {0x00, 0x50, 0x04, 0x00,0x20,//19100x60, 0x05, 0x00, 0x20, 0x70,//19200x05, 0x00, 0x40, 0x02, 0x06,//19300x00, 0x50, 0x03, 0x07, 0x00,//19400x60, 0x04, 0x00, 0x20, 0x70,//19500x05, 0x00, 0x30, 0x80, 0x06,//19600x00, 0x40, 0x03, 0x07, 0x00, 19700x50, 0x04, 0x08, 0x00, 0x60,//19800x04, 0x0a, 0x00, 0x60, 0x05,//19900x00, 0x30, 0x80, 0x05, 0x00,//20000x4 0, 0x02, 0x07, 0x00, 0x50,//20100x04, 0x09, 0x00, 0x60, 0x04,//20200x00, 0x20, 0x60, 0x05, 0x00,//20300x30, 0xb0, 0x , 0x00, 0x50,//20400x02, 0x07, 0x00, 0x50, 0x03//2050};//using matrix to store the offset days of the Gregorian and lunar calendar from 1901-2050 years of each year private static final char[ ] Offseofdays = {49, 38, 28, 46, 34, 24, 43, 32, 21, 40,//191029, 48, 36, 25, 44, 34, 22, 41, 31, 50,//192038, 27, 46, 35, 23, 43, 32, 22, 40, 29,//193047, 36, 25, 44, 34, 23, 41, 30, 49, 38,//194026, 45, 35, 24, 43, 32, 21, 40, 28, 47,//195036, 26, 44, 33, 23, 42, 30, 48 , 38, 27,//196045, 35, 24, 43, 32, 20, 39, 29, 47, 36,//197026, 45, 33, 22, 41, 30, 48, 37, 27, 46,//198035, 24, 43, 32, 50, 39, 28, 47, 36, 26,//199045, 34, 22, 40, 30, 49, 37, 27, 46, 35,//200023, 42, 31, 21, 39, 28, 48, 37, 25, 44, 201033, 23, 41, 31, 50, 39, 28, 47, 35, 24,//202042, 30, 21, 40, 28, 47, 36, 25, 43, 33,//203022, 41, 30, 49, 37, +, 204031, (+), max.,/////////////////// Determine if there are leap years return ((year% 4 = = 0) && (year%!! = 0) | | year% 400 = = 0);} Gets the number of days per month in the Gregorian calendar 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;elsereturn 28;} Elsereturn 0;} Gets the new year's offset day static int l_getoffsetofdays (int, int month, int day) {int days = 0;for (int i = 1; i < month; i++) {da Ys + = 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 & Amp 0x0f); Elsereturn (Month & 0xf0) >> 4;} Gets the lunar month of the current year, the static int l_getmonthdays (int year, int months) {int leapmonth = L_getleapmonth (year), if (Month > y) & ;& (month-12! = leapmonth) | | (Month < 0)) {System.out.println ("Incorrect month entered"); return-1;} if (month-12 = = Leapmonth) {if ((storelunarmonth[year-1901] & (0x8000 >> leapmonth)) = = 0) return 29;elseretu RN 30;} if ((Leapmonth > 0) && (Month > Leapmonth)) month++;if ((storelunarmonth[year-1901] & (0x8000 >> (month-1))) = = 0) return 29;elsereturn 30;} Gets the lunar year of the current year, static int getlunaryear (int years) {int iyeardays = 0;int leApmonth = L_getleapmonth (year), for (int i = 1; i <; i++) Iyeardays + = L_getmonthdays (year, I); if (Leapmonth > 0) IY Eardays + = L_getmonthdays (year, Leapmonth +); return iyeardays;} static int getoffsetofdays (int, 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, LEAPMO Nth + 12);} Days + = Day-1;return days;} Gregorian calendar converted to lunar static String Solartolundar (int year, int month, Int. day) {int l_day, l_month, l_year;int days = L_GETOFFSETOFDA YS (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 = n; 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;elsel_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 +), if (days <= 0) {l_month + = + 1;break;}}} l_month--;} Return "" + L_year + "-" + L_month + "-" + l_day;} Lunar Calendar converted to solar calendar 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_year, s_month);} S_month--;return "" + S_year + "-" + S_month + "-" + S_day;}} Custom weekday class Customweek {int week;private String weeks[] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};//define a string array, store the week seven days of the Week public Customweek () {///default Star The period is Sunday week = 0;} 0: Sunday 1: Monday 2: Tuesday 3: Wednesday 4: Thursday 5: Friday 6: Saturday public customweek (int w) {if ((W > 6) | | (W < 0)) {System.out.println ("Customweek out of range, I think you want Sunday"); this.week = 0;} Elsethis.week = w;} Public String toString () {return weeks[week];}} Custom date Classes class CustomDate {public int year;public int month;public int day;private static int checkyear (int. year) {//Prosecutorial input The year is within the specified year range if ((Year > 1901) && (Years < 2050)) return Year;else {System.out.println ("entered" is not between 1901~2050 , the default year is 1991 "); return 1991;}} Construction method Public CustomDate (int year, int month, int day) {this.year = Checkyear (year); this.month = Month;this.day = day;} Public customdate (int year, int month) {this.year = Checkyear (year); this.month = Month;this.day = 1;} Public CustomDate (int.) {this.year = Checkyear (year); this.month = 1;this.day = 1;} Public CustomDate () {//default initialization date is 1991-01-01this.year = 1991;this.month = 1;this.day = 1;} Public String toString () {return "" + this.year+ (This.month > 9? "" + This.month: "0" + this.month)//month in mm form + (th Is.day > 9? "+ This.day:" 0 "+ this.day);//day in DD form}public Boolean equals (CustomDate md) {return (Md.day = this.day) && (Md.month = = This.month) && (md.year = = This.year));}} Gregorian Date class, inheriting a custom date class Solar extends CustomDate {private static int checkmonth (int month) {//Check if month is more effective range if (Month > 12) {SYSTEM.OUT.PRINTLN ("entered month is out of bounds, default month is December"); return 12;} else if (Month < 1) {SYSTEM.OUT.PRINTLN ("entered month is out of bounds, default month is January"); return 1;} Elsereturn month;} private static int Checkday (int year, Int. month, int day) {//Check if date is more efficient range int imonthdays = Mutualconversion.getsolarmonthof Days (year, month), if (Day > Imonthdays) {System.out.println ("The date entered is out of bounds, the default date is" + Imonthdays + "); return imonthdays;} else if (Day < 1) {SYSTEM.OUT.PRINTLN ("entered date is out of bounds, default date is 1th"); return 1;} Elsereturn days;} The construction method of Solardate class publIC Solar (int year, int month, Int. day) {super (year); this.month = Checkmonth (month); this.day = Checkday (This.year, This.mon Th, day);} Public Solar (int. int, int month) {super (year); this.month = Checkmonth (month);} Public Solar (int.) {super (year);} Public Solar () {super ();} Outputs the public string toString () {return "" + this.year+ (This.month > 9?) as a string -"+ this.month:"-0 "+ this.month) + (This.day > 9?) "-" + this.day: "-0" + this.day);} Gets the date of the day that is the weekday public Customweek Toweek () {int day = 0;for (int i = 1901; i < year; i++) {if (mutualconversion.isleapy Earofsolar (i)) days + = 366;elsedays + = 365;} Days + = Mutualconversion.l_getoffsetofdays (year, month, day), return new Customweek ((days + 2)% 7);} Public Lunar Datetolunar () {//converts input date into lunar date int year, month, day, iDate; Lunar ld;idate = Integer.parseint (Mutualconversion.solartolundar (This.year,this.month, This.day)); year = idate/10000 month = iDate% 10000/100;day = iDate% 100;ld = new Lunar (year, month, day); return LD;} Publicvoid GetDate (int year, int month) {Calendar calendar = calendar.getinstance ();//Gets a calendar object int count = 0;//defines a count variable CA Lendar.set (Calendar.year, year);//Set Years Calendar.set (Calendar.month, month-1);//Set month Calendar.set (calendar.date, 0) ;//Set Date int days = Mutualconversion.getsolarmonthofdays (year, month);; The perpetual calendar of System.out.println (year + "+ month +") is as follows: "); System.out.println ("Sunday \t\t Monday \t\t Tuesday \t\t Wednesday \t\t Thursday \t\t Friday \t\t Saturday"); while (count < days) {Calendar.add ( Calendar.day_of_month, 1);//Set the period for adding calendars to 1int day = Calendar.gettime (). GetDay ();//Gets the number of days of the day of the calendar, for example: 0: Represents Sunday if (count = = 0) { Depending on the day of the week, enter several tabfor (int i = 0; i < day; i++) {System.out.print ("\t\t");}} if (day = = 0) {//If it is Sunday, then line System.out.println ();} String time = Mutualconversion.solartolundar (year, Month, Calendar.gettime (). GetDate ()); string[] str = time.split ("-"); Lunar la = new Lunar (Integer.parseint (str[0]), Integer.parseint (Str[1]), Integer.parseint (str[2])); String name = la.tostring (0); System.out.print (calendar.geTtime (). GetDate () + "");//Gets the number of dates in the calendar System.out.print (name + "\ T");//Gets the number of dates in the Calendar count++;}}} Lunar Date classes, inheriting custom date classes class Lunar extends CustomDate {private String upperfigure[] = {"0", "one", "two", "three", "four", "five", "six", "Seven", " Eight "," Nine "," Ten "};p rivate static int checkday (int year, int month, int day) {//check if the date is the more efficient range int imonthdays = mutualconversion. Getsolarmonthofdays (year, month), if (Day > Imonthdays) {System.out.println ("The date entered is out of bounds, the default date is" + Imonthdays + "); return Imonthdays;} else if (Day < 1) {SYSTEM.OUT.PRINTLN ("entered date is out of bounds, default date is 1th"); return 1;} Elsereturn days;} private static int Checkmonth (int year, int month) {//Check if month is more effective range if (Month >) && (month = = Mutualconversion . L_getleapmonth (year) +) {return month,} else if (month >) {System.out.println ("entered month is out of bounds, default month is December"); return 12;} else if (Month < 1) {SYSTEM.OUT.PRINTLN ("entered month is out of bounds, default month is January"); return 1;} Elsereturn month;} The construction method of the Lunardate class public Lunar (int year, int month, Int. day) {Super [year]; this.month = Checkmonth (this.year, month); this.day = Checkday (This.year, This.month, day);} Public Lunar (int year, int month) {super (year); this.month = Checkmonth (this.year, month);} Public Lunar (int.) {super (year);} Public Lunar () {super ();} Output public string toString (int n) {string Scalendar = "" As a string, if (n = = 1) {Scalendar + = "Lunar" + upperfigure[year/1000] + upperfigure[year% 1000/100]+ upperfigure[year% 100/10] + upperfigure[year% 10]+ "(" + tochineseera () + ") year";} if (month >) {month-= 12;scalendar + = "Leap";} if (month = =) Scalendar + = "Lunar", else if (month = =) Scalendar + = "Winter months"; else if (month = = 1) Scalendar + = "the Month"; elsescalend Ar + = Upperfigure[month] + "month"; if (Day >) Scalendar + = "30"; else if (Day >) Scalendar + = "20" + Upperfigure[day % 20];else if (day = =) Scalendar + = "20"; else if (Day >) Scalendar + = "Ten" + upperfigure[day% 10];elsescalendar + = "First" + Upperfigure[day];return Scalendar;} Public Cnweek Toweek () {//Get Input date day of the week int days = 0;for (int i = 1901; i < year; i++) + + Mutualconversion.getlunaryear (i);d ays + = Mutualconversion.getoffsetofdays (year, month, day), return to new Cnweek (days + 2)% 7);} Public Chineseera Tochineseera () {Return to New Chineseera (year);} Public Solar tosolardate () {//Convert to solar calendar int year, month, day, iDate; Solar sd;idate = Integer.parseint (Mutualconversion.lundartosolar (This.year,this.month, This.day)); year = idate/10000 month = iDate% 10000/100;day = iDate% 100;SD = new Solar (year, month, day); return SD;}} Class Cnweek extends Customweek {//week subclass private String scnweek[] = {"Day", "one", "two", "three", "four", "five", "six"};//call the construction method of the parent class Pu Blic Cnweek () {super ();} Public Cnweek (int week) {super (week);} Public String toString () {return ' week ' + Scnweek[this.week];}} The lunar Calendar class Chineseera {int year is expressed in the form of Zodiac. String[] Westernnotation = {"A", "B", "C", "Ding", "e", "own", "Geng", "Xin", "Ren", "GUI"}; String[] chineseeranotation = {"Son", "ugly", "Yin", "Mao", "Chen", "WU", "noon", "not", "Shen", "unitary", "Xu", "Hai"};p ublic Chineseera () {int year = 1991;} Public Chineseera (Int.) {if (year< 2050) && (Year > 1901)) this.year = Year;elsethis.year = 1991;} Public String toString () {int temp;temp = Math.Abs (year-1924); return westernnotation[temp%] + Chineseeranotation[tem P% 12];}}
Perpetual Calendar Procedure Example (lunar calendar conversion)