In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article introduces how to use CalendarView control in Android to achieve date lunar calendar conversion, the content is very detailed, interested friends can refer to, hope to be helpful to you.
Create a new project named LunarDemo
Then create a java class in the sibling of MainActivity, named Lunar, and then use our core code in this class, which will be posted at the end.
Then we modify the layout file activity_main.xml first
First we change the layout of the layout to LinnerLayout and set it to the vertical layout (orientation= "vertical"), and then add a calendar control for CalendarView, a text box for TextView and a button for Button.
The idea is to use the date control to click to select the date, and then press the get Lunar Calendar button to display the current lunar date in the text box, and pop up a Toast prompt box to show what is the current year.
Then let's go back to MainActivity.java and take a look at the implementation.
Initialize the calendar with a Calendar, and then set the onSelectedDayChange event in the calendarview calendar control to modify the selected date. When you pass the button, call the Lunar class we just built, pass in the parameters of Calendar, and get the current farm day.
Generating effect
Core class
Public class Lunar {
Private int year
Private int month
Private int day
Private boolean leap
Final static String chineseNumber [] = {"one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "eleven", "twelve"}
Static SimpleDateFormat chineseDateFormat = new SimpleDateFormat ("yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy
Final static long [] lunarInfo = new long []
{0x04bd8, 0x04ae0, 0x0a570, 0x054d5, 0x0d260, 0x0d950, 0x16554, 0x056a0, 0x09ad0, 0x055d2
0x04ae0, 0x0a5b6, 0x0a4d0, 0x0d250, 0x1d255, 0x0b540, 0x0d6a0, 0x0ada2, 0x095b0, 0x14977
0x04970, 0x0a4b0, 0x0b4b5, 0x06a50, 0x06d40, 0x1ab54, 0x02b60, 0x09570, 0x052f2, 0x04970
0x06566, 0x0d4a0, 0x0ea50, 0x06e95, 0x05ad0, 0x02b60, 0x186e3, 0x092e0, 0x1c8d7, 0x0c950
0x0d4a0, 0x1d8a6, 0x0b550, 0x056a0, 0x1a5b4, 0x025d0, 0x092d0, 0x0d2b2, 0x0a950, 0x0b557
0x06ca0, 0x0b550, 0x15355, 0x04da0, 0x0a5d0, 0x14573, 0x052d0, 0x0a9a8, 0x0e950, 0x06aa0
0x0aea6, 0x0ab50, 0x04b60, 0x0aae4, 0x0a570, 0x05260, 0x0f263, 0x0d950, 0x05b57, 0x056a0
0x096d0, 0x04dd5, 0x04ad0, 0x0a4d0, 0x0d4d4, 0x0d250, 0x0d558, 0x0b540, 0x0b5a0, 0x195a6
0x095b0, 0x049b0, 0x0a974, 0x0a4b0, 0x0b27a, 0x06a50, 0x06d40, 0x0af46, 0x0ab60, 0x09570
0x04af5, 0x04970, 0x064b0, 0x074a3, 0x0ea50, 0x06b58, 0x055c0, 0x0ab60, 0x096d5, 0x092e0
0x0c960, 0x0d954, 0x0d4a0, 0x0da50, 0x07552, 0x056a0, 0x0abb7, 0x025d0, 0x092d0, 0x0cab5
0x0a950, 0x0b4a0, 0x0baa4, 0x0ad50, 0x055d9, 0x04ba0, 0x0a5b0, 0x15176, 0x052b0, 0x0a930
0x07954, 0x06aa0, 0x0ad50, 0x05b52, 0x04b60, 0x0a6e6, 0x0a4e0, 0x0d260, 0x0ea65, 0x0d530
0x05aa0, 0x076a3, 0x096d0, 0x04bd7, 0x04ad0, 0x0a4d0, 0x1d0b6, 0x0d250, 0x0d520, 0x0dd45
0x0b5a0, 0x056d0, 0x055b2, 0x049b0, 0x0a577, 0x0a4b0, 0x0aa50, 0x1b255, 0x06d20, 0x0ada0}
/ / = the total number of days returned to the y year of the lunar calendar
Final private static int yearDays (int y) {
Int I, sum = 348
For (I = 0x8000; I > 0x8; I > > = 1) {
If ((lunarInfo [y-1900] & I)! = 0) sum + = 1
}
Return (sum + leapDays (y))
}
/ / = returns the number of days of the leap month in the y year of the lunar calendar
Final private static int leapDays (int y) {
If (leapMonth (y)! = 0) {
If ((lunarInfo [y-1900] & 0x10000)! = 0)
Return 30
Else
Return 29
} else
Return 0
}
/ / return lunar calendar y which leap month 1-12, no leap return 0
Final private static int leapMonth (int y) {
Return (int) (lunarInfo [y-1900] & 0xf)
}
/ / = the total number of days returned to the m-month of the y year of the lunar calendar
Final private static int monthDays (int y, int m) {
If ((lunarInfo [y-1900] & (0x10000 > > m)) = = 0)
Return 29
Else
Return 30
}
/ / return the zodiac in the y year of the lunar calendar
Final public String animalsYear () {
Final String [] Animals = new String [] {"rat", "cow", "tiger", "rabbit", "dragon", "snake", "horse", "sheep", "monkey", "chicken", "dog", "pig"}
Return Animals [(year-4)% 12]
}
/ / = the offset introduced on the day of the month is returned to the stem branch, 0 = Jiazi
Final private static String cyclicalm (int num) {
Final String [] Gan = new String [] {"A", "B", "C", "D", "E", "ji", "Geng", "Xin", "ren", "Gui"}
Final String [] Zhi = new String [] {"Zi", "ugly", "Yin", "Mao", "Chen", "si", "noon", "Wei", "Shen", "you", "Xu", "Hai"}
Return (Gan [num% 10] + Zhi [num% 12])
}
/ / = input offset and return stem branch, 0 = Jiazi
Final public String cyclical () {
Int num = year-1900 + 36
Return (cyclicalm (num))
}
Public String getLunarMonthString () {
/ / TODO Auto-generated method stub
Return null
}
Public Lunar (Calendar cal) {
@ SuppressWarnings ("unused") int yearCyl, monCyl, dayCyl
Int leapMonth = 0
Date baseDate = null
Try {
BaseDate = chineseDateFormat.parse ("31 January 1900")
} catch (ParseException e) {
E.printStackTrace (); / / To change body of catch statement use Options | File Templates.
}
/ / find out the number of days that differ from January 31, 1900
Int offset = (int) ((cal.getTime (). GetTime ()-baseDate.getTime ()) / 86400000L)
DayCyl = offset + 40
MonCyl = 14
/ / subtract the number of days per lunar year with offset
/ / calculate what day it is in the lunar calendar.
/ / the final result is the year of the lunar calendar
/ / offset is the day of the year
Int iYear, daysOfYear = 0
For (iYear = 1900; iYear
< 2050 && offset >0; iYear++) {
DaysOfYear = yearDays (iYear)
Offset-= daysOfYear
MonCyl + = 12
}
If (offset
< 0) { offset += daysOfYear; iYear--; monCyl -= 12; } //农历年份 year = iYear; yearCyl = iYear - 1864; leapMonth = leapMonth(iYear); //闰哪个月,1-12 leap = false; //用当年的天数offset,逐个减去每月(农历)的天数,求出当天是本月的第几天 int iMonth, daysOfMonth = 0; for (iMonth = 1; iMonth < 13 && offset >0; iMonth++) {
/ / Leap month
If (leapMonth > 0 & & iMonth = (leapMonth + 1) & &! leap) {
-- iMonth
Leap = true
DaysOfMonth = leapDays (year)
} else
DaysOfMonth = monthDays (year, iMonth)
Offset-= daysOfMonth
/ / release of leap months
If (leap & & iMonth = = (leapMonth + 1)) leap = false
If (! leap) monCyl++
}
/ / offset is 0, and the month just calculated is a leap month, which needs to be corrected.
If (offset = = 0 & & leapMonth > 0 & & iMonth = = leapMonth + 1) {
If (leap) {
Leap = false
} else {
Leap = true
-- iMonth
-- monCyl
}
}
/ / when offset is less than 0, it should also be corrected.
If (offset
< 0) { offset += daysOfMonth; --iMonth; --monCyl; } month = iMonth; day = offset + 1; } public static String getChinaDayString(int day) { String chineseTen[] = {"初", "十", "廿", "三"}; int n = day % 10 == 0 ? 9 : day % 10 - 1; if (day >30)
Return ""
If (day = = 10)
Return "the Tenth year of junior high school"
Else
Return chineseTen [day / 10] + chineseNumber [n]
}
Public String toString () {
Return (leap? "Leap": ") + chineseNumber [month-1] +" month "+ getChinaDayString (day)
}
}
On how to use CalendarView controls in Android to achieve date lunar calendar conversion is shared here, I hope the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.