├── sample
├── .gitignore
├── src
│ ├── main
│ │ ├── res
│ │ │ ├── values
│ │ │ │ ├── strings.xml
│ │ │ │ ├── dimens.xml
│ │ │ │ ├── colors.xml
│ │ │ │ └── styles.xml
│ │ │ ├── mipmap-hdpi
│ │ │ │ └── ic_launcher.png
│ │ │ ├── mipmap-mdpi
│ │ │ │ └── ic_launcher.png
│ │ │ ├── mipmap-xhdpi
│ │ │ │ └── ic_launcher.png
│ │ │ ├── mipmap-xxhdpi
│ │ │ │ └── ic_launcher.png
│ │ │ ├── mipmap-xxxhdpi
│ │ │ │ └── ic_launcher.png
│ │ │ ├── anim
│ │ │ │ ├── push_bottom_in.xml
│ │ │ │ ├── push_bottom_out.xml
│ │ │ │ ├── pop_menu_in_set.xml
│ │ │ │ └── pop_menu_out_set.xml
│ │ │ ├── values-w820dp
│ │ │ │ └── dimens.xml
│ │ │ └── layout
│ │ │ │ ├── layout_calendar_dialog.xml
│ │ │ │ └── activity_main.xml
│ │ ├── java
│ │ │ └── com
│ │ │ │ └── nanchen
│ │ │ │ └── calendarviewdemo
│ │ │ │ ├── ToastUtil.java
│ │ │ │ └── MainActivity.java
│ │ └── AndroidManifest.xml
│ ├── test
│ │ └── java
│ │ │ └── com
│ │ │ └── nanchen
│ │ │ └── calendarviewdemo
│ │ │ └── ExampleUnitTest.java
│ └── androidTest
│ │ └── java
│ │ └── com
│ │ └── nanchen
│ │ └── calendarviewdemo
│ │ └── ExampleInstrumentedTest.java
├── proguard-rules.pro
└── build.gradle
├── calendarview
├── .gitignore
├── src
│ ├── main
│ │ ├── res
│ │ │ ├── values
│ │ │ │ ├── strings.xml
│ │ │ │ └── styles.xml
│ │ │ ├── drawable-xhdpi
│ │ │ │ ├── triangle05.png
│ │ │ │ ├── triangle06.png
│ │ │ │ ├── triangle05_pressed.png
│ │ │ │ └── triangle06_pressed.png
│ │ │ ├── anim
│ │ │ │ ├── push_right_in.xml
│ │ │ │ ├── push_right_out.xml
│ │ │ │ ├── push_left_in.xml
│ │ │ │ └── push_left_out.xml
│ │ │ ├── drawable
│ │ │ │ ├── next_month_selector.xml
│ │ │ │ ├── pre_month_selector.xml
│ │ │ │ └── bg_circle.xml
│ │ │ └── layout
│ │ │ │ ├── calen_calendar_item.xml
│ │ │ │ └── calen_calendar.xml
│ │ ├── AndroidManifest.xml
│ │ └── java
│ │ │ └── com
│ │ │ └── nanchen
│ │ │ └── calendarview
│ │ │ ├── ClickDataListener.java
│ │ │ ├── SpecialCalendar.java
│ │ │ ├── MyCalendarView.java
│ │ │ ├── CalendarAdapter.java
│ │ │ └── LunarCalendar.java
│ ├── test
│ │ └── java
│ │ │ └── com
│ │ │ └── nanchen
│ │ │ └── calendarview
│ │ │ └── ExampleUnitTest.java
│ └── androidTest
│ │ └── java
│ │ └── com
│ │ └── nanchen
│ │ └── calendarview
│ │ └── ExampleInstrumentedTest.java
├── proguard-rules.pro
└── build.gradle
├── settings.gradle
├── .idea
├── copyright
│ └── profiles_settings.xml
├── encodings.xml
├── modules.xml
├── runConfigurations.xml
├── gradle.xml
├── compiler.xml
└── misc.xml
├── gradle
└── wrapper
│ ├── gradle-wrapper.jar
│ └── gradle-wrapper.properties
├── .gitignore
├── .gitattributes
├── gradle.properties
├── README.md
├── gradlew.bat
└── gradlew
/sample/.gitignore:
--------------------------------------------------------------------------------
1 | /build
2 |
--------------------------------------------------------------------------------
/calendarview/.gitignore:
--------------------------------------------------------------------------------
1 | /build
2 |
--------------------------------------------------------------------------------
/settings.gradle:
--------------------------------------------------------------------------------
1 | include ':sample', ':calendarview'
2 |
--------------------------------------------------------------------------------
/.idea/copyright/profiles_settings.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/nanchen2251/CalendarView/HEAD/gradle/wrapper/gradle-wrapper.jar
--------------------------------------------------------------------------------
/calendarview/src/main/res/values/strings.xml:
--------------------------------------------------------------------------------
1 |
2 | Calendarview
3 |
4 |
--------------------------------------------------------------------------------
/sample/src/main/res/values/strings.xml:
--------------------------------------------------------------------------------
1 |
2 | CalendarViewDemo
3 |
4 |
--------------------------------------------------------------------------------
/sample/src/main/res/mipmap-hdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/nanchen2251/CalendarView/HEAD/sample/src/main/res/mipmap-hdpi/ic_launcher.png
--------------------------------------------------------------------------------
/sample/src/main/res/mipmap-mdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/nanchen2251/CalendarView/HEAD/sample/src/main/res/mipmap-mdpi/ic_launcher.png
--------------------------------------------------------------------------------
/sample/src/main/res/mipmap-xhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/nanchen2251/CalendarView/HEAD/sample/src/main/res/mipmap-xhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/sample/src/main/res/mipmap-xxhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/nanchen2251/CalendarView/HEAD/sample/src/main/res/mipmap-xxhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/sample/src/main/res/mipmap-xxxhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/nanchen2251/CalendarView/HEAD/sample/src/main/res/mipmap-xxxhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/calendarview/src/main/res/drawable-xhdpi/triangle05.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/nanchen2251/CalendarView/HEAD/calendarview/src/main/res/drawable-xhdpi/triangle05.png
--------------------------------------------------------------------------------
/calendarview/src/main/res/drawable-xhdpi/triangle06.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/nanchen2251/CalendarView/HEAD/calendarview/src/main/res/drawable-xhdpi/triangle06.png
--------------------------------------------------------------------------------
/calendarview/src/main/res/drawable-xhdpi/triangle05_pressed.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/nanchen2251/CalendarView/HEAD/calendarview/src/main/res/drawable-xhdpi/triangle05_pressed.png
--------------------------------------------------------------------------------
/calendarview/src/main/res/drawable-xhdpi/triangle06_pressed.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/nanchen2251/CalendarView/HEAD/calendarview/src/main/res/drawable-xhdpi/triangle06_pressed.png
--------------------------------------------------------------------------------
/.idea/encodings.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | *.iml
2 | .gradle
3 | /local.properties
4 | /.idea/workspace.xml
5 | /.idea/libraries
6 | .DS_Store
7 | /build
8 | /captures
9 | .externalNativeBuild
10 |
11 |
12 | .gradle/
13 | .idea/
14 |
--------------------------------------------------------------------------------
/sample/src/main/res/values/dimens.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | 16dp
4 | 16dp
5 |
6 |
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.properties:
--------------------------------------------------------------------------------
1 | #Mon Dec 28 10:00:20 PST 2015
2 | distributionBase=GRADLE_USER_HOME
3 | distributionPath=wrapper/dists
4 | zipStoreBase=GRADLE_USER_HOME
5 | zipStorePath=wrapper/dists
6 | distributionUrl=https\://services.gradle.org/distributions/gradle-2.14.1-all.zip
7 |
--------------------------------------------------------------------------------
/sample/src/main/res/anim/push_bottom_in.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
9 |
--------------------------------------------------------------------------------
/sample/src/main/res/values/colors.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | #3F51B5
4 | #303F9F
5 | #FF4081
6 |
7 | #d7d7d7
8 |
9 |
--------------------------------------------------------------------------------
/sample/src/main/res/anim/push_bottom_out.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
9 |
10 |
--------------------------------------------------------------------------------
/calendarview/src/main/res/anim/push_right_in.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
5 |
7 |
--------------------------------------------------------------------------------
/calendarview/src/main/res/anim/push_right_out.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
5 |
7 |
--------------------------------------------------------------------------------
/calendarview/src/main/res/drawable/next_month_selector.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
--------------------------------------------------------------------------------
/calendarview/src/main/res/drawable/pre_month_selector.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
--------------------------------------------------------------------------------
/calendarview/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
3 |
4 |
8 |
9 |
10 |
11 |
12 |
--------------------------------------------------------------------------------
/calendarview/src/main/java/com/nanchen/calendarview/ClickDataListener.java:
--------------------------------------------------------------------------------
1 | package com.nanchen.calendarview;
2 |
3 | /**
4 | * 选中日期的监听事件
5 | *
6 | * @author nanchen
7 | * @fileName CalendarViewDemo
8 | * @packageName com.nanchen.calendarview
9 | * @date 2016/12/08 10:42
10 | */
11 |
12 | public interface ClickDataListener {
13 | void clickData(int year, int month, int day);
14 | }
15 |
--------------------------------------------------------------------------------
/sample/src/main/res/values-w820dp/dimens.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 | 64dp
6 |
7 |
--------------------------------------------------------------------------------
/calendarview/src/main/res/anim/push_left_in.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
8 |
9 |
13 |
14 |
--------------------------------------------------------------------------------
/calendarview/src/main/res/anim/push_left_out.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
8 |
9 |
13 |
14 |
--------------------------------------------------------------------------------
/calendarview/src/main/res/drawable/bg_circle.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
13 |
14 |
15 |
16 |
--------------------------------------------------------------------------------
/.gitattributes:
--------------------------------------------------------------------------------
1 | # Auto detect text files and perform LF normalization
2 | * text=auto
3 |
4 | # Custom for Visual Studio
5 | *.cs diff=csharp
6 |
7 | # Standard to msysgit
8 | *.doc diff=astextplain
9 | *.DOC diff=astextplain
10 | *.docx diff=astextplain
11 | *.DOCX diff=astextplain
12 | *.dot diff=astextplain
13 | *.DOT diff=astextplain
14 | *.pdf diff=astextplain
15 | *.PDF diff=astextplain
16 | *.rtf diff=astextplain
17 | *.RTF diff=astextplain
18 |
--------------------------------------------------------------------------------
/calendarview/src/main/res/layout/calen_calendar_item.xml:
--------------------------------------------------------------------------------
1 |
2 |
6 |
7 |
12 |
13 |
14 |
--------------------------------------------------------------------------------
/calendarview/src/test/java/com/nanchen/calendarview/ExampleUnitTest.java:
--------------------------------------------------------------------------------
1 | package com.nanchen.calendarview;
2 |
3 | import org.junit.Test;
4 |
5 | import static org.junit.Assert.*;
6 |
7 | /**
8 | * Example local unit test, which will execute on the development machine (host).
9 | *
10 | * @see Testing documentation
11 | */
12 | public class ExampleUnitTest {
13 | @Test
14 | public void addition_isCorrect() throws Exception {
15 | assertEquals(4, 2 + 2);
16 | }
17 | }
--------------------------------------------------------------------------------
/sample/src/test/java/com/nanchen/calendarviewdemo/ExampleUnitTest.java:
--------------------------------------------------------------------------------
1 | package com.nanchen.calendarviewdemo;
2 |
3 | import org.junit.Test;
4 |
5 | import static org.junit.Assert.*;
6 |
7 | /**
8 | * Example local unit test, which will execute on the development machine (host).
9 | *
10 | * @see Testing documentation
11 | */
12 | public class ExampleUnitTest {
13 | @Test
14 | public void addition_isCorrect() throws Exception {
15 | assertEquals(4, 2 + 2);
16 | }
17 | }
--------------------------------------------------------------------------------
/sample/src/main/res/layout/layout_calendar_dialog.xml:
--------------------------------------------------------------------------------
1 |
2 |
6 |
7 |
11 |
12 |
--------------------------------------------------------------------------------
/.idea/modules.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
--------------------------------------------------------------------------------
/calendarview/src/main/res/values/styles.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
12 |
--------------------------------------------------------------------------------
/sample/src/main/java/com/nanchen/calendarviewdemo/ToastUtil.java:
--------------------------------------------------------------------------------
1 | package com.nanchen.calendarviewdemo;
2 |
3 | import android.content.Context;
4 | import android.widget.Toast;
5 |
6 |
7 | public class ToastUtil {
8 | private static Toast mToast;
9 |
10 | public static void showToast(Context context,String desc){
11 | if (mToast == null){
12 | mToast = Toast.makeText(context.getApplicationContext(),desc,Toast.LENGTH_SHORT);
13 | }else{
14 | mToast.setText(desc);
15 | }
16 | mToast.show();
17 | }
18 | }
19 |
--------------------------------------------------------------------------------
/.idea/runConfigurations.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
11 |
12 |
--------------------------------------------------------------------------------
/sample/src/main/res/layout/activity_main.xml:
--------------------------------------------------------------------------------
1 |
2 |
9 |
10 |
15 |
16 |
--------------------------------------------------------------------------------
/sample/src/main/res/anim/pop_menu_in_set.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
13 |
14 |
18 |
19 |
--------------------------------------------------------------------------------
/sample/src/main/res/anim/pop_menu_out_set.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
13 |
14 |
18 |
19 |
20 |
--------------------------------------------------------------------------------
/.idea/gradle.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
--------------------------------------------------------------------------------
/sample/proguard-rules.pro:
--------------------------------------------------------------------------------
1 | # Add project specific ProGuard rules here.
2 | # By default, the flags in this file are appended to flags specified
3 | # in C:\Users\Administrator\AppData\Local\Android\Sdk1\Sdk/tools/proguard/proguard-android.txt
4 | # You can edit the include path and order by changing the proguardFiles
5 | # directive in build.gradle.
6 | #
7 | # For more details, see
8 | # http://developer.android.com/guide/developing/tools/proguard.html
9 |
10 | # Add any project specific keep options here:
11 |
12 | # If your project uses WebView with JS, uncomment the following
13 | # and specify the fully qualified class name to the JavaScript interface
14 | # class:
15 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview {
16 | # public *;
17 | #}
18 |
--------------------------------------------------------------------------------
/calendarview/proguard-rules.pro:
--------------------------------------------------------------------------------
1 | # Add project specific ProGuard rules here.
2 | # By default, the flags in this file are appended to flags specified
3 | # in C:\Users\Administrator\AppData\Local\Android\Sdk1\Sdk/tools/proguard/proguard-android.txt
4 | # You can edit the include path and order by changing the proguardFiles
5 | # directive in build.gradle.
6 | #
7 | # For more details, see
8 | # http://developer.android.com/guide/developing/tools/proguard.html
9 |
10 | # Add any project specific keep options here:
11 |
12 | # If your project uses WebView with JS, uncomment the following
13 | # and specify the fully qualified class name to the JavaScript interface
14 | # class:
15 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview {
16 | # public *;
17 | #}
18 |
--------------------------------------------------------------------------------
/sample/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 |
5 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
--------------------------------------------------------------------------------
/.idea/compiler.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
--------------------------------------------------------------------------------
/gradle.properties:
--------------------------------------------------------------------------------
1 | # Project-wide Gradle settings.
2 |
3 | # IDE (e.g. Android Studio) users:
4 | # Gradle settings configured through the IDE *will override*
5 | # any settings specified in this file.
6 |
7 | # For more details on how to configure your build environment visit
8 | # http://www.gradle.org/docs/current/userguide/build_environment.html
9 |
10 | # Specifies the JVM arguments used for the daemon process.
11 | # The setting is particularly useful for tweaking memory settings.
12 | org.gradle.jvmargs=-Xmx1536m
13 |
14 | # When configured, Gradle will run in incubating parallel mode.
15 | # This option should only be used with decoupled projects. More details, visit
16 | # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
17 | # org.gradle.parallel=true
18 |
--------------------------------------------------------------------------------
/calendarview/src/androidTest/java/com/nanchen/calendarview/ExampleInstrumentedTest.java:
--------------------------------------------------------------------------------
1 | package com.nanchen.calendarview;
2 |
3 | import android.content.Context;
4 | import android.support.test.InstrumentationRegistry;
5 | import android.support.test.runner.AndroidJUnit4;
6 |
7 | import org.junit.Test;
8 | import org.junit.runner.RunWith;
9 |
10 | import static org.junit.Assert.*;
11 |
12 | /**
13 | * Instrumentation test, which will execute on an Android device.
14 | *
15 | * @see Testing documentation
16 | */
17 | @RunWith(AndroidJUnit4.class)
18 | public class ExampleInstrumentedTest {
19 | @Test
20 | public void useAppContext() throws Exception {
21 | // Context of the app under test.
22 | Context appContext = InstrumentationRegistry.getTargetContext();
23 |
24 | assertEquals("com.nanchen.calendarview.test", appContext.getPackageName());
25 | }
26 | }
27 |
--------------------------------------------------------------------------------
/sample/src/androidTest/java/com/nanchen/calendarviewdemo/ExampleInstrumentedTest.java:
--------------------------------------------------------------------------------
1 | package com.nanchen.calendarviewdemo;
2 |
3 | import android.content.Context;
4 | import android.support.test.InstrumentationRegistry;
5 | import android.support.test.runner.AndroidJUnit4;
6 |
7 | import org.junit.Test;
8 | import org.junit.runner.RunWith;
9 |
10 | import static org.junit.Assert.*;
11 |
12 | /**
13 | * Instrumentation test, which will execute on an Android device.
14 | *
15 | * @see Testing documentation
16 | */
17 | @RunWith(AndroidJUnit4.class)
18 | public class ExampleInstrumentedTest {
19 | @Test
20 | public void useAppContext() throws Exception {
21 | // Context of the app under test.
22 | Context appContext = InstrumentationRegistry.getTargetContext();
23 |
24 | assertEquals("com.nanchen.calendarviewdemo", appContext.getPackageName());
25 | }
26 | }
27 |
--------------------------------------------------------------------------------
/sample/src/main/res/values/styles.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
10 |
11 |
16 |
17 |
21 |
22 |
23 |
--------------------------------------------------------------------------------
/sample/build.gradle:
--------------------------------------------------------------------------------
1 | apply plugin: 'com.android.application'
2 |
3 |
4 | android {
5 | compileSdkVersion 25
6 | buildToolsVersion "25.0.1"
7 | defaultConfig {
8 | applicationId "com.nanchen.calendarviewdemo"
9 | minSdkVersion 11
10 | targetSdkVersion 25
11 | versionCode 1
12 | versionName "1.0"
13 | testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
14 | }
15 | buildTypes {
16 | release {
17 | minifyEnabled false
18 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
19 | }
20 | }
21 | }
22 |
23 | dependencies {
24 | compile fileTree(include: ['*.jar'], dir: 'libs')
25 | androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
26 | exclude group: 'com.android.support', module: 'support-annotations'
27 | })
28 | compile 'com.android.support:appcompat-v7:25.0.1'
29 | testCompile 'junit:junit:4.12'
30 | compile project(':calendarview')
31 | }
32 |
33 |
--------------------------------------------------------------------------------
/calendarview/src/main/java/com/nanchen/calendarview/SpecialCalendar.java:
--------------------------------------------------------------------------------
1 | package com.nanchen.calendarview;
2 |
3 | import java.util.Calendar;
4 |
5 | /**
6 | * 闰年月算法
7 | *
8 | * @author nanchen
9 | * @fileName CalendarViewDemo
10 | * @packageName com.nanchen.calendarview
11 | * @date 2016/12/08 10:41
12 | */
13 |
14 | public class SpecialCalendar {
15 | private int daysOfMonth = 0; // 某月的天数
16 | private int dayOfWeek = 0; // 具体某一天是星期几
17 |
18 | // 判断是否为闰年
19 | public boolean isLeapYear(int year) {
20 | if (year % 100 == 0 && year % 400 == 0) {
21 | return true;
22 | } else if (year % 100 != 0 && year % 4 == 0) {
23 | return true;
24 | }
25 | return false;
26 | }
27 |
28 | // 得到某月有多少天数
29 | public int getDaysOfMonth(boolean isLeapYear, int month) {
30 | switch (month) {
31 | case 1:
32 | case 3:
33 | case 5:
34 | case 7:
35 | case 8:
36 | case 10:
37 | case 12:
38 | daysOfMonth = 31;
39 | break;
40 | case 4:
41 | case 6:
42 | case 9:
43 | case 11:
44 | daysOfMonth = 30;
45 | break;
46 | case 2:
47 | if (isLeapYear) {
48 | daysOfMonth = 29;
49 | } else {
50 | daysOfMonth = 28;
51 | }
52 |
53 | }
54 | return daysOfMonth;
55 | }
56 |
57 | // 指定某年中的某月的第一天是星期几
58 | public int getWeekdayOfMonth(int year, int month) {
59 | Calendar cal = Calendar.getInstance();
60 | cal.set(year, month - 1, 1);
61 | dayOfWeek = cal.get(Calendar.DAY_OF_WEEK) - 1;
62 | return dayOfWeek;
63 | }
64 | }
65 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # CalendarView
2 | # 一个自带农历和节假日的开源日历库
3 | 这是一个自带节假日和滑动的日历控件,欢迎各位拍砖
4 | ## 效果图
5 | 
6 |
7 | #### ⊙开源不易,希望给个star或者fork奖励
8 | ## 特点
9 | 1、支持ViewPager形式的左右滑动
10 | 2、支持点击效果
11 | 3、支持农历和周末的颜色显示
12 | ## 使用方法
13 | #### 1、添加依赖
14 | ```java
15 | compile 'com.nanchen.calendarview:calendarview:1.0.7'
16 | ```
17 | 或者
18 | ```java
19 |
20 | com.nanchen.calendarview
21 | calendarview
22 | 1.0.7
23 | pom
24 |
25 | ```
26 | #### 2017年1月5日后仓库迁移移到jitpack,添加依赖方式为:
27 | ##### Step 1. Add it in your root build.gradle at the end of repositories:
28 | ```java
29 | allprojects {
30 | repositories {
31 | ...
32 | maven { url 'https://jitpack.io' }
33 | }
34 | }
35 | ```
36 | ##### Step 2. Add the dependency
37 | ```java
38 | dependencies {
39 | compile 'com.github.nanchen2251:CalendarView:1.0.7'
40 | }
41 | ```
42 | #### 2、在xml文件里面使用
43 | ```java
44 |
48 | ```
49 | #### 3、在Activity里面使用
50 | ```java
51 | MyCalendarView calendarView = (MyCalendarView) window.findViewById(R.id.calendarView);
52 | calendarView.setClickDataListener(new ClickDataListener() {
53 | @Override
54 | public void clickData(int year, int month, int day) {
55 | date = String.format(Locale.CHINA, "%04d-%02d-%02d", year, month, day);
56 | Toast.showToast(MainActivity.this.getApplicationContext(),date,Toast.LENGTH_SHORT).show();
57 | }
58 | });
59 | ```
60 | ## 关于作者
61 | 南尘
62 | 四川成都
63 | [博客园](http://www.cnblogs.com/liushilin/)
64 |
--------------------------------------------------------------------------------
/sample/src/main/java/com/nanchen/calendarviewdemo/MainActivity.java:
--------------------------------------------------------------------------------
1 | package com.nanchen.calendarviewdemo;
2 |
3 | import android.app.AlertDialog;
4 | import android.os.Bundle;
5 | import android.support.v7.app.AppCompatActivity;
6 | import android.view.Gravity;
7 | import android.view.View;
8 | import android.view.ViewGroup.LayoutParams;
9 | import android.view.Window;
10 | import android.widget.Toast;
11 |
12 | import com.nanchen.calendarview.ClickDataListener;
13 | import com.nanchen.calendarview.MyCalendarView;
14 |
15 | import java.util.Locale;
16 |
17 | public class MainActivity extends AppCompatActivity {
18 |
19 | private String date;
20 |
21 | @Override
22 | protected void onCreate(Bundle savedInstanceState) {
23 | super.onCreate(savedInstanceState);
24 | setContentView(R.layout.activity_main);
25 | }
26 |
27 | public void btnClick(View view) {
28 | // final AlertDialog dialog = new AlertDialog.Builder(this).create();
29 | final AlertDialog dialog = new AlertDialog.Builder(this, R.style.dialog_style).create();
30 | dialog.show();
31 | Window window = dialog.getWindow();
32 |
33 | window.setContentView(R.layout.layout_calendar_dialog);
34 | window.setLayout(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
35 | window.setWindowAnimations(R.style.AnimBottom);
36 | window.setGravity(Gravity.BOTTOM);
37 |
38 | MyCalendarView calendarView = (MyCalendarView) window.findViewById(R.id.calendarView);
39 | calendarView.setClickDataListener(new ClickDataListener() {
40 | @Override
41 | public void clickData(int year, int month, int day) {
42 | date = String.format(Locale.CHINA, "%04d-%02d-%02d", year, month, day);
43 | ToastUtil.showToast(MainActivity.this,date);
44 | dialog.cancel();
45 | }
46 | });
47 | }
48 | }
49 |
--------------------------------------------------------------------------------
/gradlew.bat:
--------------------------------------------------------------------------------
1 | @if "%DEBUG%" == "" @echo off
2 | @rem ##########################################################################
3 | @rem
4 | @rem Gradle startup script for Windows
5 | @rem
6 | @rem ##########################################################################
7 |
8 | @rem Set local scope for the variables with windows NT shell
9 | if "%OS%"=="Windows_NT" setlocal
10 |
11 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
12 | set DEFAULT_JVM_OPTS=
13 |
14 | set DIRNAME=%~dp0
15 | if "%DIRNAME%" == "" set DIRNAME=.
16 | set APP_BASE_NAME=%~n0
17 | set APP_HOME=%DIRNAME%
18 |
19 | @rem Find java.exe
20 | if defined JAVA_HOME goto findJavaFromJavaHome
21 |
22 | set JAVA_EXE=java.exe
23 | %JAVA_EXE% -version >NUL 2>&1
24 | if "%ERRORLEVEL%" == "0" goto init
25 |
26 | echo.
27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
28 | echo.
29 | echo Please set the JAVA_HOME variable in your environment to match the
30 | echo location of your Java installation.
31 |
32 | goto fail
33 |
34 | :findJavaFromJavaHome
35 | set JAVA_HOME=%JAVA_HOME:"=%
36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe
37 |
38 | if exist "%JAVA_EXE%" goto init
39 |
40 | echo.
41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME%
42 | echo.
43 | echo Please set the JAVA_HOME variable in your environment to match the
44 | echo location of your Java installation.
45 |
46 | goto fail
47 |
48 | :init
49 | @rem Get command-line arguments, handling Windowz variants
50 |
51 | if not "%OS%" == "Windows_NT" goto win9xME_args
52 | if "%@eval[2+2]" == "4" goto 4NT_args
53 |
54 | :win9xME_args
55 | @rem Slurp the command line arguments.
56 | set CMD_LINE_ARGS=
57 | set _SKIP=2
58 |
59 | :win9xME_args_slurp
60 | if "x%~1" == "x" goto execute
61 |
62 | set CMD_LINE_ARGS=%*
63 | goto execute
64 |
65 | :4NT_args
66 | @rem Get arguments from the 4NT Shell from JP Software
67 | set CMD_LINE_ARGS=%$
68 |
69 | :execute
70 | @rem Setup the command line
71 |
72 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar
73 |
74 | @rem Execute Gradle
75 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS%
76 |
77 | :end
78 | @rem End local scope for the variables with windows NT shell
79 | if "%ERRORLEVEL%"=="0" goto mainEnd
80 |
81 | :fail
82 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of
83 | rem the _cmd.exe /c_ return code!
84 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1
85 | exit /b 1
86 |
87 | :mainEnd
88 | if "%OS%"=="Windows_NT" endlocal
89 |
90 | :omega
91 |
--------------------------------------------------------------------------------
/calendarview/src/main/res/layout/calen_calendar.xml:
--------------------------------------------------------------------------------
1 |
2 |
6 |
7 |
11 |
12 |
19 |
20 |
29 |
30 |
37 |
38 |
39 |
43 |
44 |
48 |
49 |
52 |
53 |
56 |
57 |
60 |
61 |
64 |
65 |
68 |
69 |
73 |
74 |
75 |
79 |
80 |
84 |
85 |
--------------------------------------------------------------------------------
/.idea/misc.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
43 |
44 |
45 |
46 |
47 |
48 |
49 |
50 | 1.8
51 |
52 |
53 |
54 |
55 |
56 |
57 |
58 |
59 |
60 |
61 |
62 |
--------------------------------------------------------------------------------
/gradlew:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env bash
2 |
3 | ##############################################################################
4 | ##
5 | ## Gradle start up script for UN*X
6 | ##
7 | ##############################################################################
8 |
9 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
10 | DEFAULT_JVM_OPTS=""
11 |
12 | APP_NAME="Gradle"
13 | APP_BASE_NAME=`basename "$0"`
14 |
15 | # Use the maximum available, or set MAX_FD != -1 to use that value.
16 | MAX_FD="maximum"
17 |
18 | warn ( ) {
19 | echo "$*"
20 | }
21 |
22 | die ( ) {
23 | echo
24 | echo "$*"
25 | echo
26 | exit 1
27 | }
28 |
29 | # OS specific support (must be 'true' or 'false').
30 | cygwin=false
31 | msys=false
32 | darwin=false
33 | case "`uname`" in
34 | CYGWIN* )
35 | cygwin=true
36 | ;;
37 | Darwin* )
38 | darwin=true
39 | ;;
40 | MINGW* )
41 | msys=true
42 | ;;
43 | esac
44 |
45 | # Attempt to set APP_HOME
46 | # Resolve links: $0 may be a link
47 | PRG="$0"
48 | # Need this for relative symlinks.
49 | while [ -h "$PRG" ] ; do
50 | ls=`ls -ld "$PRG"`
51 | link=`expr "$ls" : '.*-> \(.*\)$'`
52 | if expr "$link" : '/.*' > /dev/null; then
53 | PRG="$link"
54 | else
55 | PRG=`dirname "$PRG"`"/$link"
56 | fi
57 | done
58 | SAVED="`pwd`"
59 | cd "`dirname \"$PRG\"`/" >/dev/null
60 | APP_HOME="`pwd -P`"
61 | cd "$SAVED" >/dev/null
62 |
63 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar
64 |
65 | # Determine the Java command to use to start the JVM.
66 | if [ -n "$JAVA_HOME" ] ; then
67 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
68 | # IBM's JDK on AIX uses strange locations for the executables
69 | JAVACMD="$JAVA_HOME/jre/sh/java"
70 | else
71 | JAVACMD="$JAVA_HOME/bin/java"
72 | fi
73 | if [ ! -x "$JAVACMD" ] ; then
74 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME
75 |
76 | Please set the JAVA_HOME variable in your environment to match the
77 | location of your Java installation."
78 | fi
79 | else
80 | JAVACMD="java"
81 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
82 |
83 | Please set the JAVA_HOME variable in your environment to match the
84 | location of your Java installation."
85 | fi
86 |
87 | # Increase the maximum file descriptors if we can.
88 | if [ "$cygwin" = "false" -a "$darwin" = "false" ] ; then
89 | MAX_FD_LIMIT=`ulimit -H -n`
90 | if [ $? -eq 0 ] ; then
91 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then
92 | MAX_FD="$MAX_FD_LIMIT"
93 | fi
94 | ulimit -n $MAX_FD
95 | if [ $? -ne 0 ] ; then
96 | warn "Could not set maximum file descriptor limit: $MAX_FD"
97 | fi
98 | else
99 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT"
100 | fi
101 | fi
102 |
103 | # For Darwin, add options to specify how the application appears in the dock
104 | if $darwin; then
105 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\""
106 | fi
107 |
108 | # For Cygwin, switch paths to Windows format before running java
109 | if $cygwin ; then
110 | APP_HOME=`cygpath --path --mixed "$APP_HOME"`
111 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"`
112 | JAVACMD=`cygpath --unix "$JAVACMD"`
113 |
114 | # We build the pattern for arguments to be converted via cygpath
115 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null`
116 | SEP=""
117 | for dir in $ROOTDIRSRAW ; do
118 | ROOTDIRS="$ROOTDIRS$SEP$dir"
119 | SEP="|"
120 | done
121 | OURCYGPATTERN="(^($ROOTDIRS))"
122 | # Add a user-defined pattern to the cygpath arguments
123 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then
124 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)"
125 | fi
126 | # Now convert the arguments - kludge to limit ourselves to /bin/sh
127 | i=0
128 | for arg in "$@" ; do
129 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -`
130 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option
131 |
132 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition
133 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"`
134 | else
135 | eval `echo args$i`="\"$arg\""
136 | fi
137 | i=$((i+1))
138 | done
139 | case $i in
140 | (0) set -- ;;
141 | (1) set -- "$args0" ;;
142 | (2) set -- "$args0" "$args1" ;;
143 | (3) set -- "$args0" "$args1" "$args2" ;;
144 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;;
145 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;;
146 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;;
147 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;;
148 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;;
149 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;;
150 | esac
151 | fi
152 |
153 | # Split up the JVM_OPTS And GRADLE_OPTS values into an array, following the shell quoting and substitution rules
154 | function splitJvmOpts() {
155 | JVM_OPTS=("$@")
156 | }
157 | eval splitJvmOpts $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS
158 | JVM_OPTS[${#JVM_OPTS[*]}]="-Dorg.gradle.appname=$APP_BASE_NAME"
159 |
160 | exec "$JAVACMD" "${JVM_OPTS[@]}" -classpath "$CLASSPATH" org.gradle.wrapper.GradleWrapperMain "$@"
161 |
--------------------------------------------------------------------------------
/calendarview/build.gradle:
--------------------------------------------------------------------------------
1 | apply plugin: 'com.android.library'
2 | //配置插件
3 | apply plugin: 'com.github.dcendents.android-maven'
4 | //apply plugin: 'com.jfrog.bintray'
5 | group='com.github.nanchen2251' // 指定group,com.github.<用户名>
6 |
7 | android {
8 | compileSdkVersion 25
9 | buildToolsVersion "25.0.1"
10 |
11 | defaultConfig {
12 | minSdkVersion 11
13 | targetSdkVersion 25
14 | versionCode 1
15 | versionName "1.0"
16 |
17 | testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
18 |
19 | }
20 | buildTypes {
21 | release {
22 | minifyEnabled false
23 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
24 | }
25 | }
26 |
27 | lintOptions {
28 | abortOnError false
29 | }
30 |
31 | }
32 | //tasks.withType(JavaCompile) {
33 | // options.encoding = "UTF-8"
34 | //}
35 | //tasks.withType(Javadoc) {
36 | // options.encoding = "UTF-8"
37 | //}
38 |
39 |
40 | dependencies {
41 | compile fileTree(dir: 'libs', include: ['*.jar'])
42 | androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
43 | exclude group: 'com.android.support', module: 'support-annotations'
44 | })
45 | compile 'com.android.support:appcompat-v7:25.0.1'
46 | testCompile 'junit:junit:4.12'
47 | }
48 |
49 | // build a jar with source files
50 | task sourcesJar(type: Jar) {
51 | from android.sourceSets.main.java.srcDirs
52 | classifier = 'sources'
53 | }
54 |
55 | task javadoc(type: Javadoc) {
56 | failOnError false
57 | source = android.sourceSets.main.java.sourceFiles
58 | classpath += project.files(android.getBootClasspath().join(File.pathSeparator))
59 | classpath += configurations.compile
60 | }
61 |
62 | // build a jar with javadoc
63 | task javadocJar(type: Jar, dependsOn: javadoc) {
64 | classifier = 'javadoc'
65 | from javadoc.destinationDir
66 | }
67 |
68 | artifacts {
69 | archives sourcesJar
70 | archives javadocJar
71 | }
72 |
73 | //version = "1.0.7" //这个是版本号,必须填写
74 | //def siteUrl = 'https://github.com/nanchen2251/CalendarView' // 项目的主页
75 | //def gitUrl = 'https://github.com/nanchen2251/CalendarView' // Git仓库的url
76 | //
77 | //group = "com.nanchen.calendarview" // 这里是groupId ,必须填写 一般填你唯一的包名
78 | //
79 | //install {
80 | // repositories.mavenInstaller {
81 | // // This generates POM.xml with proper parameters
82 | // pom {
83 | // project {
84 | // packaging 'aar'
85 | // // 项目描述,复制我的话,这里需要修改。
86 | // name 'a view with the lunar calendar' //项目描述
87 | // url siteUrl
88 | // // 软件开源协议,现在一般都是Apache License2.0吧,复制我的,这里不需要修改。
89 | // licenses {
90 | // license {
91 | // name 'The Apache Software License, Version 2.0'
92 | // url 'http://www.apache.org/licenses/LICENSE-2.0.txt'
93 | // }
94 | // }
95 | // //填写开发者基本信息,复制我的,这里需要修改。
96 | // developers {
97 | // developer {
98 | // id 'nanchen' //你公司的id
99 | // name 'nanchen2251' //你的用户名
100 | // email 'liushilin520@foxmail.com' // 你的邮箱
101 | // }
102 | // }
103 | //
104 | // // SCM,复制我的,这里不需要修改。
105 | // scm {
106 | // connection gitUrl
107 | // developerConnection gitUrl
108 | // url siteUrl
109 | // }
110 | // }
111 | // }
112 | // }
113 | //}
114 | //// 生成jar包的task,不需要修改。
115 | //task sourcesJar(type: Jar) {
116 | // from android.sourceSets.main.java.srcDirs
117 | // classifier = 'sources'
118 | //}
119 | //// 生成javaDoc的jar,不需要修改
120 | //task javadoc(type: Javadoc) {
121 | // options.encoding = "UTF-8"
122 | // source = android.sourceSets.main.java.srcDirs
123 | // classpath += project.files(android.getBootClasspath().join(File.pathSeparator))
124 | //}
125 | //task javadocJar(type: Jar, dependsOn: javadoc) {
126 | // classifier = 'javadoc'
127 | // from javadoc.destinationDir
128 | //}
129 | //
130 | ////下面设置编码格式,重点注意,如果不设置可能会在gradlew install的时候出现GBK编码映射错误
131 | //javadoc {
132 | // options {
133 | // encoding "UTF-8"
134 | // charSet 'UTF-8'
135 | // author true
136 | // version true
137 | // links "http://docs.oracle.com/javase/7/docs/api"
138 | // title 'A CalendarView Support Lunar Calendar For Android' // 文档标题
139 | // }
140 | //}
141 | //
142 | //artifacts {
143 | //// archives javadocJar
144 | // archives sourcesJar
145 | //}
146 | //
147 | //// 生成jar包
148 | //task releaseJar(type: Copy) {
149 | // from( 'build/intermediates/bundles/release')
150 | // into( '../jar')
151 | // include('classes.jar')
152 | // rename('classes.jar', 'okgo-' + version + '.jar')
153 | //}
154 | //
155 | //// 这里是读取Bintray相关的信息,我们上传项目到github上的时候会把gradle文件传上去,
156 | //// 所以不要把帐号密码的信息直接写在这里,写在local.properties中,这里动态读取。
157 | //Properties properties = new Properties()
158 | //properties.load(project.rootProject.file('local.properties').newDataInputStream())
159 | //bintray {
160 | //
161 | // //读取 local.properties 文件里面的 bintray.user
162 | // user = properties.getProperty("bintray.user")
163 | //
164 | // //读取 local.properties 文件里面的 bintray.apikey
165 | // key = properties.getProperty("bintray.apikey")
166 | //
167 | // configurations = ['archives']
168 | // pkg {
169 | // userOrg = "nanchen" //发布到JCenter的组织,注意新版本的bintray是需要手动创建的
170 | // repo = "maven" //发布到JCenter上的仓库名称,注意新版本的bintray是需要手动创建的
171 | // // 发布到Bintray上的项目名字
172 | // name = "calendarview-library"
173 | // websiteUrl = siteUrl
174 | // vcsUrl = gitUrl
175 | // licenses = ["Apache-2.0"]
176 | // publish = true // 是否是公开项目
177 | // }
178 | //}
--------------------------------------------------------------------------------
/calendarview/src/main/java/com/nanchen/calendarview/MyCalendarView.java:
--------------------------------------------------------------------------------
1 | package com.nanchen.calendarview;
2 |
3 | import android.annotation.TargetApi;
4 | import android.app.Activity;
5 | import android.content.Context;
6 | import android.content.ContextWrapper;
7 | import android.graphics.Color;
8 | import android.graphics.drawable.ColorDrawable;
9 | import android.os.Build.VERSION_CODES;
10 | import android.util.AttributeSet;
11 | import android.util.Log;
12 | import android.view.Display;
13 | import android.view.GestureDetector;
14 | import android.view.GestureDetector.SimpleOnGestureListener;
15 | import android.view.Gravity;
16 | import android.view.MotionEvent;
17 | import android.view.View;
18 | import android.view.View.OnClickListener;
19 | import android.view.WindowManager;
20 | import android.view.animation.AnimationUtils;
21 | import android.widget.AdapterView;
22 | import android.widget.AdapterView.OnItemClickListener;
23 | import android.widget.GridView;
24 | import android.widget.ImageView;
25 | import android.widget.LinearLayout;
26 | import android.widget.TextView;
27 | import android.widget.ViewFlipper;
28 |
29 | import java.text.SimpleDateFormat;
30 | import java.util.Date;
31 |
32 | /**
33 | * @author nanchen
34 | * @fileName CalendarViewDemo
35 | * @packageName com.nanchen.calendarview
36 | * @date 2016/12/08 10:39
37 | */
38 |
39 | public class MyCalendarView extends LinearLayout implements OnClickListener {
40 |
41 | private final String TAG = MyCalendarView.class.getSimpleName();
42 | private int year_c = 0;// 今天的年份
43 | private int month_c = 0;// 今天的月份
44 | private int day_c = 0;// 今天的日期
45 | private String currentDate = "";
46 | private Context mContext;
47 | private TextView currentMonth;// 显示日期
48 | private ImageView prevMonth;// 去上一个月
49 | private ImageView nextMonth;// 去下一个月
50 | private int gvFlag = 0;
51 | private GestureDetector gestureDetector = null;
52 | private CalendarAdapter calV = null;
53 | private ViewFlipper flipper = null;
54 | private GridView gridView = null;
55 | private static int jumpMonth = 0; // 每次滑动,增加或减去一个月,默认为0(即显示当前月)
56 | private static int jumpYear = 0; // 滑动跨越一年,则增加或者减去一年,默认为0(即当前年)
57 | private ClickDataListener clickDataListener;
58 |
59 | public MyCalendarView(Context context) {
60 | this(context, null);
61 | }
62 |
63 | public MyCalendarView(Context context, AttributeSet attrs) {
64 | super(context, attrs);
65 | mContext = context;
66 | initView();
67 | }
68 |
69 | @TargetApi(VERSION_CODES.HONEYCOMB)
70 | public MyCalendarView(Context context, AttributeSet attrs, int defStyleAttr) {
71 | super(context, attrs, defStyleAttr);
72 | mContext = context;
73 | initView();
74 | }
75 |
76 | private void initView() {
77 | View view = View.inflate(mContext, R.layout.calen_calendar, this);
78 | currentMonth = (TextView) view.findViewById(R.id.currentMonth);
79 | prevMonth = (ImageView) view.findViewById(R.id.prevMonth);
80 | nextMonth = (ImageView) view.findViewById(R.id.nextMonth);
81 | setListener();
82 | setCurrentDay();
83 | gestureDetector = new GestureDetector(mContext, new MyGestureListener());
84 | flipper = (ViewFlipper) findViewById(R.id.flipper);
85 | flipper.removeAllViews();
86 | calV = new CalendarAdapter(mContext, getResources(), jumpMonth,
87 | jumpYear, year_c, month_c, day_c);
88 | addGridView();
89 | gridView.setAdapter(calV);
90 | flipper.addView(gridView, 0);
91 | addTextToTopTextView(currentMonth);
92 | }
93 |
94 | private void setListener() {
95 | prevMonth.setOnClickListener(this);
96 | nextMonth.setOnClickListener(this);
97 |
98 | }
99 |
100 | public void setClickDataListener(ClickDataListener clickDataListener) {
101 | this.clickDataListener = clickDataListener;
102 | }
103 |
104 | private void setCurrentDay() {
105 | Date date = new Date();
106 | SimpleDateFormat sdf = new SimpleDateFormat("yyyy-M-dd");
107 | currentDate = sdf.format(date); // 当期日期
108 | year_c = Integer.parseInt(currentDate.split("-")[0]);
109 | month_c = Integer.parseInt(currentDate.split("-")[1]);
110 | day_c = Integer.parseInt(currentDate.split("-")[2]);
111 | }
112 |
113 | /**
114 | * 移动到下一个月
115 | *
116 | * @param gvFlag
117 | */
118 | private void enterNextMonth(int gvFlag) {
119 | addGridView(); // 添加一个gridView
120 | jumpMonth++; // 下一个月
121 | calV = new CalendarAdapter(mContext, this.getResources(), jumpMonth,
122 | jumpYear, year_c, month_c, day_c);
123 | gridView.setAdapter(calV);
124 | addTextToTopTextView(currentMonth); // 移动到下一月后,将当月显示在头标题中
125 | gvFlag++;
126 | flipper.addView(gridView, gvFlag);
127 | flipper.setInAnimation(AnimationUtils.loadAnimation(mContext,
128 | R.anim.push_left_in));
129 | flipper.setOutAnimation(AnimationUtils.loadAnimation(mContext,
130 | R.anim.push_left_out));
131 | flipper.showNext();
132 | flipper.removeViewAt(0);
133 | }
134 |
135 | /**
136 | * 移动到上一个月
137 | *
138 | * @param gvFlag
139 | */
140 | private void enterPrevMonth(int gvFlag) {
141 | addGridView(); // 添加一个gridView
142 | jumpMonth--; // 上一个月
143 |
144 | calV = new CalendarAdapter(mContext, this.getResources(), jumpMonth,
145 | jumpYear, year_c, month_c, day_c);
146 | gridView.setAdapter(calV);
147 | gvFlag++;
148 | addTextToTopTextView(currentMonth); // 移动到上一月后,将当月显示在头标题中
149 | flipper.addView(gridView, gvFlag);
150 |
151 | flipper.setInAnimation(AnimationUtils.loadAnimation(mContext,
152 | R.anim.push_right_in));
153 | flipper.setOutAnimation(AnimationUtils.loadAnimation(mContext,
154 | R.anim.push_right_out));
155 | flipper.showPrevious();
156 | flipper.removeViewAt(0);
157 | }
158 |
159 | /**
160 | * 添加头部的年份 闰哪月等信息
161 | *
162 | * @param view
163 | */
164 | private void addTextToTopTextView(TextView view) {
165 | StringBuffer textDate = new StringBuffer();
166 | // draw = getResources().getDrawable(R.drawable.top_day);
167 | // view.setBackgroundDrawable(draw);
168 | textDate.append(calV.getShowYear()).append("年")
169 | .append(calV.getShowMonth()).append("月").append("\t");
170 | view.setText(textDate);
171 | }
172 |
173 | private void addGridView() {
174 | LayoutParams params = new LayoutParams(
175 | LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
176 | // 取得屏幕的宽度和高度
177 | // WindowManager windowManager = ((Activity) mContext).getWindowManager();
178 |
179 | WindowManager windowManager = scanForActivity(mContext).getWindowManager();
180 | Display display = windowManager.getDefaultDisplay();
181 | int Width = display.getWidth();
182 | int Height = display.getHeight();
183 |
184 | gridView = new GridView(mContext);
185 | gridView.setNumColumns(7);
186 | gridView.setColumnWidth(40);
187 | // gridView.setStretchMode(GridView.STRETCH_COLUMN_WIDTH);
188 | if (Width == 720 && Height == 1280) {
189 | gridView.setColumnWidth(40);
190 | }
191 | gridView.setGravity(Gravity.CENTER_VERTICAL);
192 | gridView.setSelector(new ColorDrawable(Color.TRANSPARENT));
193 | // 去除gridView边框
194 | gridView.setVerticalSpacing(0);
195 | gridView.setHorizontalSpacing(0);
196 | gridView.setOnTouchListener(new OnTouchListener() {
197 | // 将gridview中的触摸事件回传给gestureDetector
198 |
199 | public boolean onTouch(View v, MotionEvent event) {
200 | // TODO Auto-generated method stub
201 | return gestureDetector.onTouchEvent(event);
202 | }
203 | });
204 |
205 | gridView.setOnItemClickListener(new OnItemClickListener() {
206 |
207 | @Override
208 | public void onItemClick(AdapterView> arg0, View arg1,
209 | int position, long arg3) {
210 | // TODO Auto-generated method stub
211 | // 点击任何一个item,得到这个item的日期(排除点击的是周日到周六(点击不响应))
212 | int startPosition = calV.getStartPositon();
213 | int endPosition = calV.getEndPosition();
214 | if (startPosition <= position + 7
215 | && position <= endPosition - 7) {
216 | int scheduleDay = Integer.parseInt(calV.getDateByClickItem(position)
217 | .split("\\.")[0]); // 这一天的阳历
218 | int scheduleYear = Integer.parseInt(calV.getShowYear());
219 | int scheduleMonth = Integer.parseInt(calV.getShowMonth());
220 | ((CalendarAdapter) arg0.getAdapter())
221 | .setColorDataPosition(position);
222 | if (clickDataListener != null) {
223 | clickDataListener.clickData(scheduleYear,
224 | scheduleMonth, scheduleDay);
225 | }
226 | }
227 | }
228 | });
229 | gridView.setLayoutParams(params);
230 | }
231 |
232 | @Override
233 | public void onClick(View v) {
234 | int i = v.getId();
235 | if (i == R.id.nextMonth) {
236 | enterNextMonth(gvFlag);
237 | Log.d(TAG, "gvFlag=" + gvFlag);
238 |
239 | } else if (i == R.id.prevMonth) {
240 | enterPrevMonth(gvFlag);
241 |
242 | }
243 |
244 | }
245 |
246 | class MyGestureListener extends SimpleOnGestureListener {
247 | @Override
248 | public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
249 | float velocityY) {
250 | int gvFlag = 0; // 每次添加gridview到viewflipper中时给的标记
251 | if (e1.getX() - e2.getX() > 120) {
252 | // 像左滑动
253 | enterNextMonth(gvFlag);
254 | return true;
255 | } else if (e1.getX() - e2.getX() < -120) {
256 | // 向右滑动
257 | enterPrevMonth(gvFlag);
258 | return true;
259 | }
260 | return false;
261 | }
262 | }
263 |
264 | private Activity scanForActivity(Context context){
265 | if (context == null ){
266 | return null;
267 | }else if (context instanceof Activity){
268 | return (Activity)context;
269 | }else if (context instanceof ContextWrapper){
270 | return scanForActivity(((ContextWrapper)context).getBaseContext());
271 | }
272 | return null;
273 | }
274 | }
275 |
--------------------------------------------------------------------------------
/calendarview/src/main/java/com/nanchen/calendarview/CalendarAdapter.java:
--------------------------------------------------------------------------------
1 | package com.nanchen.calendarview;
2 |
3 | import android.content.Context;
4 | import android.content.res.Resources;
5 | import android.graphics.Color;
6 | import android.text.SpannableString;
7 | import android.text.Spanned;
8 | import android.text.style.RelativeSizeSpan;
9 | import android.text.style.StyleSpan;
10 | import android.util.Log;
11 | import android.view.LayoutInflater;
12 | import android.view.View;
13 | import android.view.ViewGroup;
14 | import android.widget.BaseAdapter;
15 | import android.widget.TextView;
16 |
17 | import java.text.SimpleDateFormat;
18 | import java.util.Date;
19 |
20 | /**
21 | * @author nanchen
22 | * @fileName CalendarViewDemo
23 | * @packageName com.nanchen.calendarview
24 | * @date 2016/12/08 10:40
25 | */
26 |
27 | public class CalendarAdapter extends BaseAdapter {
28 | private boolean isLeapyear = false; // 是否为闰年
29 | private int daysOfMonth = 0; // 某月的天数
30 | private int dayOfWeek = 0; // 具体某一天是星期几
31 | private int lastDaysOfMonth = 0; // 上一个月的总天数
32 | private Context context;
33 | private String[] dayNumber = new String[42]; // 一个gridview中的日期存入此数组中
34 | private SpecialCalendar sc = null;
35 | private LunarCalendar lc = null;
36 | private Resources res = null;
37 | private String currentYear = "";
38 | private String currentMonth = "";
39 | private String currentDay = "";
40 |
41 | private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-M-dd");
42 | private String showYear = ""; // 用于在头部显示的年份
43 | private String showMonth = ""; // 用于在头部显示的月份
44 | private String animalsYear = "";
45 | private String leapMonth = ""; // 闰哪一个月
46 | private String cyclical = ""; // 天干地支
47 | // 系统当前时间
48 | private String sysDate = "";
49 | private String sys_year = "";
50 | private String sys_month = "";
51 | private String sys_day = "";
52 | private int colorDataPosition = -1;
53 | private boolean isClickData = false;
54 |
55 | public CalendarAdapter() {
56 | Date date = new Date();
57 | sysDate = sdf.format(date); // 当期日期
58 | sys_year = sysDate.split("-")[0];
59 | sys_month = sysDate.split("-")[1];
60 | sys_day = sysDate.split("-")[2];
61 |
62 | }
63 |
64 | public CalendarAdapter(Context context, Resources rs, int jumpMonth,
65 | int jumpYear, int year_c, int month_c, int day_c) {
66 | this();
67 | this.context = context;
68 | sc = new SpecialCalendar();
69 | lc = new LunarCalendar();
70 | this.res = rs;
71 |
72 | int stepYear = year_c + jumpYear;
73 | int stepMonth = month_c + jumpMonth;
74 | if (stepMonth > 0) {
75 | // 往下一个月滑动
76 | if (stepMonth % 12 == 0) {
77 | stepYear = year_c + stepMonth / 12 - 1;
78 | stepMonth = 12;
79 | } else {
80 | stepYear = year_c + stepMonth / 12;
81 | stepMonth = stepMonth % 12;
82 | }
83 | } else {
84 | // 往上一个月滑动
85 | stepYear = year_c - 1 + stepMonth / 12;
86 | stepMonth = stepMonth % 12 + 12;
87 | if (stepMonth % 12 == 0) {
88 |
89 | }
90 | }
91 |
92 | currentYear = String.valueOf(stepYear); // 得到当前的年份
93 | currentMonth = String.valueOf(stepMonth); // 得到本月
94 | // (jumpMonth为滑动的次数,每滑动一次就增加一月或减一月)
95 | currentDay = String.valueOf(day_c); // 得到当前日期是哪天
96 |
97 | getCalendar(Integer.parseInt(currentYear),
98 | Integer.parseInt(currentMonth));
99 |
100 | }
101 |
102 | public CalendarAdapter(Context context, Resources rs, int year, int month,
103 | int day) {
104 | this();
105 | this.context = context;
106 | sc = new SpecialCalendar();
107 | lc = new LunarCalendar();
108 | this.res = rs;
109 | currentYear = String.valueOf(year);// 得到跳转到的年份
110 | currentMonth = String.valueOf(month); // 得到跳转到的月份
111 | currentDay = String.valueOf(day); // 得到跳转到的天
112 | getCalendar(Integer.parseInt(currentYear),
113 | Integer.parseInt(currentMonth));
114 | }
115 |
116 | @Override
117 | public int getCount() {
118 | // TODO Auto-generated method stub
119 | return dayNumber.length;
120 | }
121 |
122 | @Override
123 | public Object getItem(int position) {
124 | // TODO Auto-generated method stub
125 | return position;
126 | }
127 |
128 | @Override
129 | public long getItemId(int position) {
130 | // TODO Auto-generated method stub
131 | return position;
132 | }
133 |
134 | @Override
135 | public View getView(int position, View convertView, ViewGroup parent) {
136 |
137 | if (convertView == null) {
138 | convertView = LayoutInflater.from(context).inflate(
139 | R.layout.calen_calendar_item, null);
140 | }
141 | TextView textView = (TextView) convertView.findViewById(R.id.tvtext);
142 | String d = dayNumber[position].split("\\.")[0];
143 | String dv = dayNumber[position].split("\\.")[1];
144 |
145 | SpannableString sp = new SpannableString(d + "\n" + dv);
146 | sp.setSpan(new StyleSpan(android.graphics.Typeface.BOLD), 0,
147 | d.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
148 | sp.setSpan(new RelativeSizeSpan(1.2f), 0, d.length(),
149 | Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
150 | if (dv != null || dv != "") {
151 | sp.setSpan(new RelativeSizeSpan(0.75f), d.length() + 1,
152 | dayNumber[position].length(),
153 | Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
154 | }
155 | textView.setText(sp);
156 | textView.setTextColor(Color.GRAY);
157 |
158 | if (position < daysOfMonth + dayOfWeek && position >= dayOfWeek) {
159 | // 当前月信息显示
160 | textView.setTextColor(Color.BLACK);// 当月字体设黑
161 | // drawable = new ColorDrawable(Color.rgb(23, 126, 214));
162 | if (position % 7 == 0 || position % 7 == 6) {
163 | // 当前月信息显示
164 | textView.setTextColor(Color.rgb(23, 126, 214));// 当月字体设黑
165 | }
166 | }
167 |
168 | if (colorDataPosition == position) {
169 | // 设置当天的背景
170 | textView.setTextColor(Color.WHITE);
171 |
172 | textView.setBackgroundResource(R.drawable.bg_circle);
173 | } else {
174 | textView.setBackgroundColor(res
175 | .getColor(android.R.color.transparent));
176 | }
177 | return convertView;
178 | }
179 |
180 | // 得到某年的某月的天数且这月的第一天是星期几
181 | private void getCalendar(int year, int month) {
182 | isLeapyear = sc.isLeapYear(year); // 是否为闰年
183 | daysOfMonth = sc.getDaysOfMonth(isLeapyear, month); // 某月的总天数
184 | dayOfWeek = sc.getWeekdayOfMonth(year, month); // 某月第一天为星期几
185 | lastDaysOfMonth = sc.getDaysOfMonth(isLeapyear, month - 1); // 上一个月的总天数
186 | getweek(year, month);
187 | }
188 |
189 | // 将一个月中的每一天的值添加入数组dayNuber中
190 | private void getweek(int year, int month) {
191 | int j = 1;
192 | String lunarDay = "";
193 | // 得到当前月的所有日程日期(这些日期需要标记)
194 | for (int i = 0; i < dayNumber.length; i++) {
195 | if (i < dayOfWeek) { // 前一个月
196 | int temp = lastDaysOfMonth - dayOfWeek + 1;
197 | lunarDay = lc.getLunarDate(year, month - 1, temp + i, false);
198 | dayNumber[i] = (temp + i) + "." + lunarDay;
199 |
200 | } else if (i < daysOfMonth + dayOfWeek) { // 本月
201 | String day = String.valueOf(i - dayOfWeek + 1); // 得到的日期
202 | lunarDay = lc.getLunarDate(year, month, i - dayOfWeek + 1,
203 | false);
204 | dayNumber[i] = i - dayOfWeek + 1 + "." + lunarDay;
205 | // 对于当前月才去标记当前日期
206 | if (sys_year.equals(String.valueOf(year))
207 | && sys_month.equals(String.valueOf(month))
208 | && sys_day.equals(day)) {
209 | // 标记当前日期
210 | colorDataPosition = i;
211 | }
212 | setShowYear(String.valueOf(year));
213 | setShowMonth(String.valueOf(month));
214 | setAnimalsYear(lc.animalsYear(year));
215 | setLeapMonth(lc.leapMonth == 0 ? "" : String
216 | .valueOf(lc.leapMonth));
217 | setCyclical(lc.cyclical(year));
218 | } else { // 下一个月
219 | lunarDay = lc.getLunarDate(year, month + 1, j, false);
220 | dayNumber[i] = j + "." + lunarDay;
221 | j++;
222 | }
223 | }
224 |
225 | String abc = "";
226 | for (int i = 0; i < dayNumber.length; i++) {
227 | abc = abc + dayNumber[i] + ":";
228 | }
229 | Log.d("DAYNUMBER", abc);
230 |
231 | }
232 |
233 | public void matchScheduleDate(int year, int month, int day) {
234 |
235 | }
236 |
237 | /**
238 | * 点击每一个item时返回item中的日期
239 | *
240 | * @param position
241 | * @return
242 | */
243 | public String getDateByClickItem(int position) {
244 | return dayNumber[position];
245 | }
246 |
247 | /**
248 | * 在点击gridView时,得到这个月中第一天的位置
249 | *
250 | * @return
251 | */
252 | public int getStartPositon() {
253 | return dayOfWeek + 7;
254 | }
255 |
256 | /**
257 | * 在点击gridView时,得到这个月中最后一天的位置
258 | *
259 | * @return
260 | */
261 | public int getEndPosition() {
262 | return (dayOfWeek + daysOfMonth + 7) - 1;
263 | }
264 |
265 | public String getShowYear() {
266 | return showYear;
267 | }
268 |
269 | public void setShowYear(String showYear) {
270 | this.showYear = showYear;
271 | }
272 |
273 | public String getShowMonth() {
274 | return showMonth;
275 | }
276 |
277 | public void setShowMonth(String showMonth) {
278 | this.showMonth = showMonth;
279 | }
280 |
281 | public String getAnimalsYear() {
282 | return animalsYear;
283 | }
284 |
285 | public void setAnimalsYear(String animalsYear) {
286 | this.animalsYear = animalsYear;
287 | }
288 |
289 | public String getLeapMonth() {
290 | return leapMonth;
291 | }
292 |
293 | public void setLeapMonth(String leapMonth) {
294 | this.leapMonth = leapMonth;
295 | }
296 |
297 | public String getCyclical() {
298 | return cyclical;
299 | }
300 |
301 | public void setCyclical(String cyclical) {
302 | this.cyclical = cyclical;
303 | }
304 |
305 | /**
306 | * @param position
307 | * 设置点击的日期的颜色位置
308 | */
309 | public void setColorDataPosition(int position) {
310 | if (position >= dayOfWeek && position < daysOfMonth + dayOfWeek) {
311 | colorDataPosition = position;
312 | notifyDataSetChanged();
313 | }
314 | }
315 | }
316 |
--------------------------------------------------------------------------------
/calendarview/src/main/java/com/nanchen/calendarview/LunarCalendar.java:
--------------------------------------------------------------------------------
1 | package com.nanchen.calendarview;
2 |
3 | import java.text.ParseException;
4 | import java.text.SimpleDateFormat;
5 | import java.util.Date;
6 | import java.util.Locale;
7 |
8 | /**
9 | * 农历算法
10 | *
11 | * @author nanchen
12 | * @fileName CalendarViewDemo
13 | * @packageName com.nanchen.calendarview
14 | * @date 2016/12/08 10:43
15 | */
16 |
17 | public class LunarCalendar {
18 | private int year; // 农历的年份
19 | private int month;
20 | private int day;
21 | private String lunarMonth; // 农历的月份
22 | public int leapMonth = 0; // 闰的是哪个月
23 |
24 | final static String chineseNumber[] = { "一", "二", "三", "四", "五", "六", "七", "八", "九", "十", "十一", "十二" };
25 | static SimpleDateFormat chineseDateFormat = new SimpleDateFormat("yyyy年MM月dd日", Locale.CHINA);
26 | final static long[] lunarInfo = new long[] { //
27 | 0x04bd8, 0x04ae0, 0x0a570, 0x054d5, 0x0d260, 0x0d950, 0x16554, 0x056a0, 0x09ad0, //
28 | 0x055d2, 0x04ae0, 0x0a5b6, 0x0a4d0, 0x0d250, 0x1d255, 0x0b540, 0x0d6a0, 0x0ada2, //
29 | 0x095b0, 0x14977, 0x04970, 0x0a4b0, 0x0b4b5, 0x06a50, 0x06d40, 0x1ab54, 0x02b60, //
30 | 0x09570, 0x052f2, 0x04970, 0x06566, 0x0d4a0, 0x0ea50, 0x06e95, 0x05ad0, 0x02b60, //
31 | 0x186e3, 0x092e0, 0x1c8d7, 0x0c950, 0x0d4a0, 0x1d8a6, 0x0b550, 0x056a0, 0x1a5b4, //
32 | 0x025d0, 0x092d0, 0x0d2b2, 0x0a950, 0x0b557, 0x06ca0, 0x0b550, 0x15355, 0x04da0, //
33 | 0x0a5d0, 0x14573, 0x052d0, 0x0a9a8, 0x0e950, 0x06aa0, 0x0aea6, 0x0ab50, 0x04b60, //
34 | 0x0aae4, 0x0a570, 0x05260, 0x0f263, 0x0d950, 0x05b57, 0x056a0, 0x096d0, 0x04dd5, //
35 | 0x04ad0, 0x0a4d0, 0x0d4d4, 0x0d250, 0x0d558, 0x0b540, 0x0b5a0, 0x195a6, 0x095b0, //
36 | 0x049b0, 0x0a974, 0x0a4b0, 0x0b27a, 0x06a50, 0x06d40, 0x0af46, 0x0ab60, 0x09570, //
37 | 0x04af5, 0x04970, 0x064b0, 0x074a3, 0x0ea50, 0x06b58, 0x055c0, 0x0ab60, 0x096d5, //
38 | 0x092e0, 0x0c960, 0x0d954, 0x0d4a0, 0x0da50, 0x07552, 0x056a0, 0x0abb7, 0x025d0, //
39 | 0x092d0, 0x0cab5, 0x0a950, 0x0b4a0, 0x0baa4, 0x0ad50, 0x055d9, 0x04ba0, 0x0a5b0, //
40 | 0x15176, 0x052b0, 0x0a930, 0x07954, 0x06aa0, 0x0ad50, 0x05b52, 0x04b60, 0x0a6e6, //
41 | 0x0a4e0, 0x0d260, 0x0ea65, 0x0d530, 0x05aa0, 0x076a3, 0x096d0, 0x04bd7, 0x04ad0, //
42 | 0x0a4d0, 0x1d0b6, 0x0d250, 0x0d520, 0x0dd45, 0x0b5a0, 0x056d0, 0x055b2, 0x049b0, //
43 | 0x0a577, 0x0a4b0, 0x0aa50, 0x1b255, 0x06d20, 0x0ada0 };
44 |
45 | // 农历部分假日
46 | final static String[] lunarHoliday = new String[] { "0101 春节", "0115 元宵", "0505 端午", "0707 情人", "0715 中元", "0815 中秋", "0909 重阳", "1208 腊八", "1224 小年", "0100 除夕" };
47 |
48 | // 公历部分节假日
49 | final static String[] solarHoliday = new String[] { //
50 | "0101 元旦", "0214 情人", "0308 妇女", "0312 植树", "0315 消费者权益日", "0401 愚人", "0501 劳动", "0504 青年", //
51 | "0512 护士", "0601 儿童", "0701 建党", "0801 建军", "0808 父亲", "0909 毛泽东逝世纪念", "0910 教师", "0928 孔子诞辰",//
52 | "1001 国庆", "1006 老人", "1024 联合国日", "1112 孙中山诞辰纪念", "1220 澳门回归纪念", "1225 圣诞", "1226 毛泽东诞辰纪念" };
53 |
54 | // ====== 传回农历 y年的总天数
55 | final private static int yearDays(int y) {
56 | int i, sum = 348;
57 | for (i = 0x8000; i > 0x8; i >>= 1) {
58 | if ((lunarInfo[y - 1900] & i) != 0)
59 | sum += 1;
60 | }
61 | return (sum + leapDays(y));
62 | }
63 |
64 | // ====== 传回农历 y年闰月的天数
65 | final private static int leapDays(int y) {
66 | if (leapMonth(y) != 0) {
67 | if ((lunarInfo[y - 1900] & 0x10000) != 0)
68 | return 30;
69 | else
70 | return 29;
71 | } else
72 | return 0;
73 | }
74 |
75 | // ====== 传回农历 y年闰哪个月 1-12 , 没闰传回 0
76 | final private static int leapMonth(int y) {
77 | int result = (int) (lunarInfo[y - 1900] & 0xf);
78 | return result;
79 | }
80 |
81 | // ====== 传回农历 y年m月的总天数
82 | final private static int monthDays(int y, int m) {
83 | if ((lunarInfo[y - 1900] & (0x10000 >> m)) == 0)
84 | return 29;
85 | else
86 | return 30;
87 | }
88 |
89 | // ====== 传回农历 y年的生肖
90 | final public String animalsYear(int year) {
91 | final String[] Animals = new String[] { "鼠", "牛", "虎", "兔", "龙", "蛇", "马", "羊", "猴", "鸡", "狗", "猪" };
92 | return Animals[(year - 4) % 12];
93 | }
94 |
95 | // ====== 传入 月日的offset 传回干支, 0=甲子
96 | final private static String cyclicalm(int num) {
97 | final String[] Gan = new String[] { "甲", "乙", "丙", "丁", "戊", "己", "庚", "辛", "壬", "癸" };
98 | final String[] Zhi = new String[] { "子", "丑", "寅", "卯", "辰", "巳", "午", "未", "申", "酉", "戌", "亥" };
99 | return (Gan[num % 10] + Zhi[num % 12]);
100 | }
101 |
102 | // ====== 传入 offset 传回干支, 0=甲子
103 | final public String cyclical(int year) {
104 | int num = year - 1900 + 36;
105 | return (cyclicalm(num));
106 | }
107 |
108 | public static String getChinaDayString(int day) {
109 | String chineseTen[] = { "初", "十", "廿", "卅" };
110 | int n = day % 10 == 0 ? 9 : day % 10 - 1;
111 | if (day > 30)
112 | return "";
113 | if (day == 10)
114 | return "初十";
115 | else
116 | return chineseTen[day / 10] + chineseNumber[n];
117 | }
118 |
119 | /**
120 | * 传出y年m月d日对应的农历. yearCyl3:农历年与1864的相差数 ? monCyl4:从1900年1月31日以来,闰月数
121 | * dayCyl5:与1900年1月31日相差的天数,再加40 ?
122 | *
123 | * isday: 这个参数为false---日期为节假日时,阴历日期就返回节假日 ,true---不管日期是否为节假日依然返回这天对应的阴历日期
124 | *
125 | * @return
126 | */
127 | public String getLunarDate(int year_log, int month_log, int day_log,
128 | boolean isday) {
129 | // @SuppressWarnings("unused")
130 | int yearCyl, monCyl, dayCyl;
131 | // int leapMonth = 0;
132 | String nowadays;
133 | Date baseDate = null;
134 | Date nowaday = null;
135 | try {
136 | baseDate = chineseDateFormat.parse("1900年1月31日");
137 | } catch (ParseException e) {
138 | e.printStackTrace(); // To change body of catch statement use
139 | // Options | File Templates.
140 | }
141 |
142 | nowadays = year_log + "年" + month_log + "月" + day_log + "日";
143 | try {
144 | nowaday = chineseDateFormat.parse(nowadays);
145 | } catch (ParseException e) {
146 | e.printStackTrace(); // To change body of catch statement use
147 | // Options | File Templates.
148 | }
149 |
150 | // 求出和1900年1月31日相差的天数
151 | int offset = (int) ((nowaday.getTime() - baseDate.getTime()) / 86400000L);
152 | dayCyl = offset + 40;
153 | monCyl = 14;
154 |
155 | // 用offset减去每农历年的天数
156 | // 计算当天是农历第几天
157 | // i最终结果是农历的年份
158 | // offset是当年的第几天
159 | int iYear, daysOfYear = 0;
160 | for (iYear = 1900; iYear < 10000 && offset > 0; iYear++) {
161 | daysOfYear = yearDays(iYear);
162 | offset -= daysOfYear;
163 | monCyl += 12;
164 | }
165 | if (offset < 0) {
166 | offset += daysOfYear;
167 | iYear--;
168 | monCyl -= 12;
169 | }
170 | // 农历年份
171 | year = iYear;
172 | setYear(year); // 设置公历对应的农历年份
173 |
174 | yearCyl = iYear - 1864;
175 | leapMonth = leapMonth(iYear); // 闰哪个月,1-12
176 | boolean leap = false;
177 |
178 | // 用当年的天数offset,逐个减去每月(农历)的天数,求出当天是本月的第几天
179 | int iMonth, daysOfMonth = 0;
180 | for (iMonth = 1; iMonth < 13 && offset > 0; iMonth++) {
181 | // 闰月
182 | if (leapMonth > 0 && iMonth == (leapMonth + 1) && !leap) {
183 | --iMonth;
184 | leap = true;
185 | daysOfMonth = leapDays(year);
186 | } else
187 | daysOfMonth = monthDays(year, iMonth);
188 |
189 | offset -= daysOfMonth;
190 | // 解除闰月
191 | if (leap && iMonth == (leapMonth + 1))
192 | leap = false;
193 | if (!leap)
194 | monCyl++;
195 | }
196 | // offset为0时,并且刚才计算的月份是闰月,要校正
197 | if (offset == 0 && leapMonth > 0 && iMonth == leapMonth + 1) {
198 | if (leap) {
199 | leap = false;
200 | } else {
201 | leap = true;
202 | --iMonth;
203 | --monCyl;
204 | }
205 | }
206 | // offset小于0时,也要校正
207 | if (offset < 0) {
208 | offset += daysOfMonth;
209 | --iMonth;
210 | --monCyl;
211 | }
212 | month = iMonth;
213 | setLunarMonth(chineseNumber[month - 1] + "月"); // 设置对应的阴历月份
214 | day = offset + 1;
215 |
216 | if (!isday) {
217 | // 如果日期为节假日则阴历日期则返回节假日
218 | // setLeapMonth(leapMonth);
219 | for (int i = 0; i < solarHoliday.length; i++) {
220 | // 返回公历节假日名称
221 | String sd = solarHoliday[i].split(" ")[0]; // 节假日的日期
222 | String sdv = solarHoliday[i].split(" ")[1]; // 节假日的名称
223 | String smonth_v = month_log + "";
224 | String sday_v = day_log + "";
225 | String smd = "";
226 | if (month_log < 10) {
227 | smonth_v = "0" + month_log;
228 | }
229 | if (day_log < 10) {
230 | sday_v = "0" + day_log;
231 | }
232 | smd = smonth_v + sday_v;
233 | if (sd.trim().equals(smd.trim())) {
234 | return sdv;
235 | }
236 | }
237 |
238 | for (int i = 0; i < lunarHoliday.length; i++) {
239 | // 返回农历节假日名称
240 | String ld = lunarHoliday[i].split(" ")[0]; // 节假日的日期
241 | String ldv = lunarHoliday[i].split(" ")[1]; // 节假日的名称
242 | String lmonth_v = month + "";
243 | String lday_v = day + "";
244 | String lmd = "";
245 | if (month < 10) {
246 | lmonth_v = "0" + month;
247 | }
248 | if (day < 10) {
249 | lday_v = "0" + day;
250 | }
251 | lmd = lmonth_v + lday_v;
252 | if (ld.trim().equals(lmd.trim())) {
253 | return ldv;
254 | }
255 | }
256 | }
257 | if (day == 1)
258 | return chineseNumber[month - 1] + "月";
259 | else
260 | return getChinaDayString(day);
261 |
262 | }
263 |
264 | public String toString() {
265 | if (chineseNumber[month - 1] == "一" && getChinaDayString(day) == "初一")
266 | return "农历" + year + "年";
267 | else if (getChinaDayString(day) == "初一")
268 | return chineseNumber[month - 1] + "月";
269 | else
270 | return getChinaDayString(day);
271 | // return year + "年" + (leap ? "闰" : "") + chineseNumber[month - 1] +
272 | // "月" + getChinaDayString(day);
273 | }
274 |
275 | /*
276 | * public static void main(String[] args) { System.out.println(new
277 | * LunarCalendar().getLunarDate(2012, 1, 23)); }
278 | */
279 |
280 | public int getLeapMonth() {
281 | return leapMonth;
282 | }
283 |
284 | public void setLeapMonth(int leapMonth) {
285 | this.leapMonth = leapMonth;
286 | }
287 |
288 | /**
289 | * 得到当前日期对应的阴历月份
290 | *
291 | * @return
292 | */
293 | public String getLunarMonth() {
294 | return lunarMonth;
295 | }
296 |
297 | public void setLunarMonth(String lunarMonth) {
298 | this.lunarMonth = lunarMonth;
299 | }
300 |
301 | /**
302 | * 得到当前年对应的农历年份
303 | *
304 | * @return
305 | */
306 | public int getYear() {
307 | return year;
308 | }
309 |
310 | public void setYear(int year) {
311 | this.year = year;
312 | }
313 | }
314 |
--------------------------------------------------------------------------------