Androidカレンダーmaterial-calendarview旧暦
16645 ワード
implementation 'com.prolificinteractive:material-calendarview:1.4.2'
package com.wlh.calendarview;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import com.prolificinteractive.materialcalendarview.CalendarDay;
import com.prolificinteractive.materialcalendarview.CalendarMode;
import com.prolificinteractive.materialcalendarview.MaterialCalendarView;
import com.prolificinteractive.materialcalendarview.OnMonthChangedListener;
import com.prolificinteractive.materialcalendarview.format.DayFormatter;
import com.prolificinteractive.materialcalendarview.format.TitleFormatter;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private static final String TAG = "MainActivity";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final MaterialCalendarView calendarView = (MaterialCalendarView) findViewById(R.id.calendar_calendarView);
CalendarDay date = CalendarDay.today();
calendarView.setCurrentDate(date);
calendarView.setShowOtherDates(MaterialCalendarView.SHOW_OTHER_MONTHS);
calendarView
.state()
.edit()
//
.setFirstDayOfWeek(Calendar.SUNDAY)
//
.setCalendarDisplayMode(CalendarMode.MONTHS)
.commit();
//
calendarView.setWeekDayLabels(new String[]{" ", " ", " ", " ", " ", " ", " "});
// title
calendarView.setTitleFormatter(new TitleFormatter() {
@Override
public CharSequence format(CalendarDay day) {
StringBuffer buffer = new StringBuffer();
int yearOne = day.getYear();
int monthOne = day.getMonth() + 1;
buffer.append(yearOne).append(" ").append(monthOne).append(" ");
return buffer;
}
});
calendarView.setOnMonthChangedListener(new OnMonthChangedListener() {
@Override
public void onMonthChanged(MaterialCalendarView widget, CalendarDay date) {
CalendarDay temp;
List lunarDecorators = new ArrayList<>();
for (int i = 18; i <= 31; i++) {
temp = CalendarDay.from(date.getYear(), date.getMonth() - 1, i);
add(temp, lunarDecorators);
}
for (int i = 0; i <= 31; i++) {
temp = CalendarDay.from(date.getYear(), date.getMonth(), i);
add(temp, lunarDecorators);
}
for (int i = 0; i < 14; i++) {
temp = CalendarDay.from(date.getYear(), date.getMonth() + 1, i);
add(temp, lunarDecorators);
}
calendarView.addDecorators(lunarDecorators);
}
private void add(CalendarDay date, List lunarDecorators) {
LunarDecorator decorator = new LunarDecorator(date);
lunarDecorators.add(decorator);
}
});
}
}
package com.wlh.calendarview;
import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; import java.util.Locale;
/**
public class LunarCalendar {
private int year; //
private int month;
private int day;
private String lunarMonth; //
public int leapMonth = 0; //
final static String chineseNumber[] = { " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " " };
static SimpleDateFormat chineseDateFormat = new SimpleDateFormat("yyyy MM dd ", Locale.CHINA);
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 };
//
final static String[] lunarHoliday = new String[] { "0101 ", "0115 ", "0505 ", "0707 ", "0715 ", "0815 ", "0909 ", "1208 ", "1224 ", "0100 " };
//
final static String[] solarHoliday = new String[] { //
"0101 ", "0214 ", "0308 ", "0312 ", "0315 ", "0401 ", "0501 ", "0504 ", //
"0512 ", "0601 ", "0701 ", "0801 ", "0808 ", "0909 ", "0910 ", "0928 ",//
"1001 ", "1006 ", "1024 ", "1112 ", "1220 ", "1225 ", "1226 " };
// ====== y
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));
}
// ====== y
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;
}
// ====== y 1-12 , 0
final private static int leapMonth(int y) {
int result = (int) (lunarInfo[y - 1900] & 0xf);
return result;
}
// ====== y m
final private static int monthDays(int y, int m) {
if ((lunarInfo[y - 1900] & (0x10000 >> m)) == 0)
return 29;
else
return 30;
}
// ====== y
final public String animalsYear(int year) {
final String[] Animals = new String[] { " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " " };
return Animals[(year - 4) % 12];
}
// ====== offset , 0=
final private static String cyclicalm(int num) {
final String[] Gan = new String[] { " ", " ", " ", " ", " ", " ", " ", " ", " ", " " };
final String[] Zhi = new String[] { " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " " };
return (Gan[num % 10] + Zhi[num % 12]);
}
// ====== offset , 0=
final public String cyclical(int year) {
int num = year - 1900 + 36;
return (cyclicalm(num));
}
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 " ";
else
return chineseTen[day / 10] + chineseNumber[n];
}
/**
* y m d . yearCyl3: 1864 ? monCyl4: 1900 1 31 ,
* dayCyl5: 1900 1 31 , 40 ?
*
* isday: false--- , ,true---
*
* @return
*/
public String getLunarDate(int year_log, int month_log, int day_log,
boolean isday) {
// @SuppressWarnings("unused")
int yearCyl, monCyl, dayCyl;
// int leapMonth = 0;
String nowadays;
Date baseDate = null;
Date nowaday = null;
try {
baseDate = chineseDateFormat.parse("1900 1 31 ");
} catch (ParseException e) {
e.printStackTrace(); // To change body of catch statement use
// Options | File Templates.
}
nowadays = year_log + " " + month_log + " " + day_log + " ";
try {
nowaday = chineseDateFormat.parse(nowadays);
} catch (ParseException e) {
e.printStackTrace(); // To change body of catch statement use
// Options | File Templates.
}
// 1900 1 31
int offset = (int) ((nowaday.getTime() - baseDate.getTime()) / 86400000L);
dayCyl = offset + 40;
monCyl = 14;
// offset
//
// i
// offset
int iYear, daysOfYear = 0;
for (iYear = 1900; iYear < 10000 && offset > 0; iYear++) {
daysOfYear = yearDays(iYear);
offset -= daysOfYear;
monCyl += 12;
}
if (offset < 0) {
offset += daysOfYear;
iYear--;
monCyl -= 12;
}
//
year = iYear;
setYear(year); //
yearCyl = iYear - 1864;
leapMonth = leapMonth(iYear); // ,1-12
boolean leap = false;
// offset, ( ) ,
int iMonth, daysOfMonth = 0;
for (iMonth = 1; iMonth < 13 && offset > 0; iMonth++) {
//
if (leapMonth > 0 && iMonth == (leapMonth + 1) && !leap) {
--iMonth;
leap = true;
daysOfMonth = leapDays(year);
} else
daysOfMonth = monthDays(year, iMonth);
offset -= daysOfMonth;
//
if (leap && iMonth == (leapMonth + 1))
leap = false;
if (!leap)
monCyl++;
}
// offset 0 , ,
if (offset == 0 && leapMonth > 0 && iMonth == leapMonth + 1) {
if (leap) {
leap = false;
} else {
leap = true;
--iMonth;
--monCyl;
}
}
// offset 0 ,
if (offset < 0) {
offset += daysOfMonth;
--iMonth;
--monCyl;
}
month = iMonth;
setLunarMonth(chineseNumber[month - 1] + " "); //
day = offset + 1;
if (!isday) {
//
// setLeapMonth(leapMonth);
for (int i = 0; i < solarHoliday.length; i++) {
//
String sd = solarHoliday[i].split(" ")[0]; //
String sdv = solarHoliday[i].split(" ")[1]; //
String smonth_v = month_log + "";
String sday_v = day_log + "";
String smd = "";
if (month_log < 10) {
smonth_v = "0" + month_log;
}
if (day_log < 10) {
sday_v = "0" + day_log;
}
smd = smonth_v + sday_v;
if (sd.trim().equals(smd.trim())) {
return sdv;
}
}
for (int i = 0; i < lunarHoliday.length; i++) {
//
String ld = lunarHoliday[i].split(" ")[0]; //
String ldv = lunarHoliday[i].split(" ")[1]; //
String lmonth_v = month + "";
String lday_v = day + "";
String lmd = "";
if (month < 10) {
lmonth_v = "0" + month;
}
if (day < 10) {
lday_v = "0" + day;
}
lmd = lmonth_v + lday_v;
if ("12".equals(lmonth_v)) { //
if ((daysOfMonth == 29 && day == 29) || (daysOfMonth == 30 && day == 30)) {
return ldv;
}
}
if (ld.trim().equals(lmd.trim())) {
return ldv;
}
}
}
if (day == 1)
return chineseNumber[month - 1] + " ";
else
return getChinaDayString(day);
}
public String toString() {
if (chineseNumber[month - 1] == " " && getChinaDayString(day) == " ")
return " " + year + " ";
else if (getChinaDayString(day) == " ")
return chineseNumber[month - 1] + " ";
else
return getChinaDayString(day);
// return year + " " + (leap ? " " : "") + chineseNumber[month - 1] +
// " " + getChinaDayString(day);
}
/*
* public static void main(String[] args) { System.out.println(new
* LunarCalendar().getLunarDate(2012, 1, 23)); }
*/
public int getLeapMonth() {
return leapMonth;
}
public void setLeapMonth(int leapMonth) {
this.leapMonth = leapMonth;
}
/**
*
*
* @return
*/
public String getLunarMonth() {
return lunarMonth;
}
public void setLunarMonth(String lunarMonth) {
this.lunarMonth = lunarMonth;
}
/**
*
*
* @return
*/
public int getYear() {
return year;
}
public void setYear(int year) {
this.year = year;
}
}
package com.wlh.calendarview;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.text.style.LineBackgroundSpan;
import android.util.Log;
import com.prolificinteractive.materialcalendarview.CalendarDay;
import com.prolificinteractive.materialcalendarview.DayViewDecorator;
import com.prolificinteractive.materialcalendarview.DayViewFacade;
import java.util.Calendar;
import java.util.Date;
/**
* : on 2016/12/20 0020 14:27
* :[email protected]
*/
public class LunarDecorator implements DayViewDecorator {
private CalendarDay day;
public LunarDecorator(CalendarDay day) {
this.day = day;
}
@Override
public boolean shouldDecorate(CalendarDay day) {
if (day.getDate().equals(this.day.getDate())) {
this.day = day;
return true;
}
return false;
}
@Override
public void decorate(DayViewFacade view) {
view.addSpan(new LineBackgroundSpan() {
@Override
public void drawBackground(Canvas c, Paint p, int left, int right, int top, int baseline, int bottom, CharSequence text, int start, int end, int lnum) {
Paint paint = new Paint();
paint.setTextSize(20);
paint.setColor(Color.parseColor("#D81B60"));
String time = new LunarCalendar().getLunarDate(day.getYear(), day.getMonth()+1,day.getDay(), false);
c.drawText(time, (right - left) / 2 - 20, (bottom - top) / 2 + 17*2, paint);
}
});
}
}