自定义WheelView实现日期时间选择器

android 提供的DatePicker和TimePicker有时在开发时不满足开发需求,需要自定义WheelView来实现日期时间选择器。
参考Android竖直滑动选择器WheelView的实现

自定义wheelView类

public class WheelView1 extends ScrollView {
    public static final String TAG = WheelView1.class.getSimpleName();
    private static final int SCROLL_DIRECTION_UP = 0;
    private static final int SCROLL_DIRECTION_DOWN = 1;

    private Context mContext;
    private LinearLayout mLlView;
    private int displayItemCount; // 每页显示的数量
    private int itemHeight = 0;
    private int selectedIndex = 1;
    private int scrollDirection = -1;
    public int nCurHeight;
    private Paint paint;
    private int viewWidth;
    private int initialY;
    private int newCheck = 50;
    private List<String> mItemList;
    private Runnable scrollerTask;

    public static class OnWheelViewListener {
        public void onSelected(int selectedIndex, String item) {
        }

        public void onClick(int onClickIndex, String item) {
        }
    }

    public WheelView1(Context context) {
        super(context);
        init(context);
    }

    public WheelView1(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public WheelView1(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init(context);
    }

    private List<String> getmItemList() {
        return mItemList;
    }

    public void setmItemList(List<String> list) {
        if (null == mItemList) {
            mItemList = new ArrayList<String>();
        }
        mItemList.clear();
        mItemList.addAll(list);

        // 前面和后面补全
        for (int i = 0; i < offset; i++) {
            mItemList.add(0, "");
            mItemList.add("");
        }
        initData(0);
    }

    public static final int OFF_SET_DEFAULT = 2;
    private int offset = OFF_SET_DEFAULT; // 偏移量(需要在最前面和最后面补全)

    public int getOffset() {
        return offset;
    }

    public void setOffset(int offset) {
        this.offset = offset;
    }

    private void init(Context context) {
        mContext = context;
        Log.d(TAG, "parent: " + this.getParent());
        this.setVerticalScrollBarEnabled(false);

        mLlView = new LinearLayout(context);
        mLlView.setOrientation(LinearLayout.VERTICAL);
        this.addView(mLlView);

        scrollerTask = new Runnable() {


            public void run() {
                int newY = getScrollY();
                if (initialY - newY == 0) { // stopped
                    final int remainder = initialY % itemHeight;
                    final int divided = initialY / itemHeight;
                    if (remainder == 0) {
                        selectedIndex = divided + offset;

                        onSelectedCallBack();
                    } else {
                        if (remainder > itemHeight / 2) {
                            WheelView1.this.post(new Runnable() {
                                @Override
                                public void run() {
                                    WheelView1.this.smoothScrollTo(0, initialY - remainder + itemHeight);
                                    selectedIndex = divided + offset + 1;
                                    onSelectedCallBack();
                                }
                            });
                        } else {
                            WheelView1.this.post(new Runnable() {
                                @Override
                                public void run() {
                                    WheelView1.this.smoothScrollTo(0, initialY - remainder);
                                    selectedIndex = divided + offset;
                                    onSelectedCallBack();
                                }
                            });
                        }
                    }
                } else {
                    initialY = getScrollY();
                    WheelView1.this.postDelayed(scrollerTask, newCheck);
                }
            }
        };
        // 默认初始不滑动时执行一次回调
        if (null != onWheelViewListener) {
            onWheelViewListener.onSelected(selectedIndex, mItemList.get(selectedIndex));
        }
    }

    public void startScrollerTask() {

        initialY = getScrollY();
        this.postDelayed(scrollerTask, newCheck);
    }

    private void initData(int position) {
        displayItemCount = offset * 2 + 1;

        mLlView.removeAllViews();
        for (String item : mItemList) {
            mLlView.addView(createView(item));
        }

        refreshItemView(position);
    }

    private TextView createView(String item) {
        if (mContext == null) {
            return null;
        }
        if (item == null) {
            return new TextView(mContext);
        }

        TextView tv = new TextView(mContext);
        tv.setLayoutParams(new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT
                , ViewGroup.LayoutParams.WRAP_CONTENT));
        tv.setSingleLine(true);
        tv.setTextSize(TypedValue.COMPLEX_UNIT_SP, 28);
        tv.setText(item);
        tv.setGravity(Gravity.CENTER);
        int padding = dip2px(20);
        tv.setPadding(padding, padding, padding, padding);
        if (0 == itemHeight) {
            itemHeight = getViewMeasuredHeight(tv);    
           //itemHeight = getViewMeasuredHeight(tv) - dip2px(2); 若没有间隔线
            Log.d(TAG, "itemHeight: " + itemHeight);
            mLlView.setLayoutParams(
                    new LayoutParams(
                            ViewGroup.LayoutParams.WRAP_CONTENT
                            , itemHeight * displayItemCount
                            , Gravity.CENTER_HORIZONTAL));
            mLlView.setGravity(Gravity.CENTER);
            LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) this.getLayoutParams();
            this.setLayoutParams(new LinearLayout.LayoutParams(lp.width, itemHeight * displayItemCount));
        }

        tv.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                selectedIndex = mItemList.indexOf(item);
                if (onWheelViewListener != null) {
                    setSelection(selectedIndex - 1);
                    onWheelViewListener.onClick(selectedIndex, item);
                }
            }
        });
        return tv;
    }

    @Override
    protected void onScrollChanged(int l, int t, int oldl, int oldt) {
        super.onScrollChanged(l, t, oldl, oldt);
        nCurHeight = t;
        refreshItemView(t);
//        LogUtil.d(TAG, "onScrollChanged vertical t= " + t);
        if (t > oldt) {
//            Log.d(TAG, "向下滚动");
            scrollDirection = SCROLL_DIRECTION_DOWN;
        } else {
//            Log.d(TAG, "向上滚动");
            scrollDirection = SCROLL_DIRECTION_UP;
        }
    }

    public void refreshItemView(int y) {
        if (itemHeight == 0) {
            return;
        }
        int position = y / itemHeight + offset;
        int remainder = y % itemHeight;
        int divided = y / itemHeight;

        if (remainder == 0) {
            position = divided + offset;
        } else {
            if (remainder > itemHeight / 2) {
                position = divided + offset + 1;
            }
        }
//        LogUtil.d(TAG, "refreshItemView y= " + y);
//        LogUtil.d(TAG, "refreshItemView position= " + position);
        int childSize = mLlView.getChildCount();
        for (int i = 0; i < childSize; i++) {
            TextView itemView = (TextView) mLlView.getChildAt(i);
            if (null == itemView) {
                return;
            }
//            LogUtil.d(TAG, " position=  " + position + " i= " + i);
            if (position == i) {
                itemView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 30);
                itemView.setTextColor(mContext.getResources().getColor(R.color.white, null));
            } else {
                itemView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 28);
                itemView.setTextColor(mContext.getResources().getColor(R.color.half_trans, null));
            }
        }
    }

    /**
     * 获取选中区域的边界
     */
    int[] selectedAreaBorder;

    private int[] obtainSelectedAreaBorder() {
        if (null == selectedAreaBorder) {
            selectedAreaBorder = new int[2];
            selectedAreaBorder[0] = itemHeight * offset;
            selectedAreaBorder[1] = itemHeight * (offset + 1) + dip2px(10);
        }
        return selectedAreaBorder;
    }

    @Override
    public void setBackground(Drawable background) {

        if (viewWidth == 0) {
            viewWidth = mLlView.getWidth();
            Log.d(TAG, "viewWidth: " + viewWidth);
        }

        if (null == paint) {
            paint = new Paint();
        }
        paint.setColor(mContext.getResources().getColor(R.color.trans_color, null));
        paint.setStrokeWidth(dip2px(1f));

        background = new Drawable() {
            @Override
            public void draw(Canvas canvas) {
                if (canvas == null) {
                    return;
                }
                Log.d(TAG, "Drawable: ");
                canvas.drawLine(viewWidth / 6, obtainSelectedAreaBorder()[0]
                        , viewWidth * 5 / 6, obtainSelectedAreaBorder()[0], paint);
                canvas.drawLine(viewWidth / 6, obtainSelectedAreaBorder()[1]
                        , viewWidth * 5 / 6, obtainSelectedAreaBorder()[1], paint);
            }

            @Override
            public void setAlpha(int alpha) {
            }

            @Override
            public void setColorFilter(ColorFilter cf) {
            }

            @Override
            public int getOpacity() {
                return PixelFormat.UNKNOWN;
            }
        };
        super.setBackground(background);
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        Log.d(TAG, "w: " + w + ", h: " + h + ", oldw: " + oldw + ", oldh: " + oldh);
        viewWidth = w;
        setBackground(null);
    }

    /**
     * 选中回调
     */
    private void onSelectedCallBack() {
        if (null != onWheelViewListener) {
            onWheelViewListener.onSelected(selectedIndex, mItemList.get(selectedIndex));
        }

    }

    public void setSelection(int position) {
        final int p = position;
        selectedIndex = p + offset;
        this.post(new Runnable() {
            @Override
            public void run() {
                WheelView1.this.smoothScrollTo(0, p * itemHeight);
            }
        });

    }

    public String getSeletedItem() {
        return mItemList.get(selectedIndex);
    }

    public int getSeletedIndex() {
        return selectedIndex - offset;
    }


    @Override
    public void fling(int velocityY) {
        super.fling(velocityY / 3);
    }

    @Override
    public boolean onTouchEvent(MotionEvent ev) {
        if (ev.getAction() == MotionEvent.ACTION_UP) {

            startScrollerTask();
        }
        return super.onTouchEvent(ev);
    }

    private OnWheelViewListener onWheelViewListener;

    public OnWheelViewListener getOnWheelViewListener() {
        return onWheelViewListener;
    }

    public void setOnWheelViewListener(OnWheelViewListener onWheelViewListener) {
        this.onWheelViewListener = onWheelViewListener;
        // 默认初始不滑动时执行一次回调
        onWheelViewListener.onSelected(selectedIndex, mItemList.get(selectedIndex));

        onWheelViewListener.onClick(selectedIndex, mItemList.get(selectedIndex));
    }

    private int dip2px(float dpValue) {
        final float scale = mContext.getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }

    private int getViewMeasuredHeight(View view) {
        int width = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);
        int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2,
                MeasureSpec.AT_MOST);
        view.measure(width, expandSpec);
        return view.getMeasuredHeight();
    }
}

主要方法

public class ManualSetTimeFragment extends Fragment {
    private static final String TAG = "ManualSetTimeFragment";
    private WheelView1 mYearWheelView;
    private WheelView1 mMonthWheelView;
    private WheelView1 mDayWheelView;
    private WheelView1 mHourWheel;
    private WheelView1 mMinWheel;
    private WheelView1 mAmPmWheel;
    private String year, month, day;
    private Calendar calendar;
    private int amPm, hour, min;
    private int maxHour = 23;

 @Nullable
 @Override
  public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        layoutInflater = inflater;
        rootView = inflater.inflate(R.layout.pickerview_option_hour, container, false);
        initViews(rootView);
        initEvents();
    }
   
    protected void initViews(View view) {
        if (null == view) {
            return;
        }
        mYearWheelView = view.findViewById(R.id.year);
        mMonthWheelView = view.findViewById(R.id.month);
        mDayWheelView = view.findViewById(R.id.day);
        mHourWheel = view.findViewById(R.id.hour);
        mMinWheel = view.findViewById(R.id.min);
        mAmPmWheel = view.findViewById(R.id.morning_and_afternoon);
         }
    protected void initEvents() {
        calendar = Calendar.getInstance();
        year = String.valueOf(calendar.get(Calendar.YEAR));
        month = String.valueOf(calendar.get(Calendar.MONTH));
        day = String.valueOf(calendar.get(Calendar.DAY_OF_MONTH));
        initDate();
        initTime();
    }

    private void initDate() {
        //加载年
        List<String> yearList = new ArrayList<>();
        for (int i = 2018; i < 2079; i++) {
            yearList.add(String.valueOf(i));
        }
        List<String> monthList = new ArrayList<>();
        for (int i = 1; i < 13; i++) {
            monthList.add(String.valueOf(i));
        }
        mYearWheelView.setOffset(2);
        if (Integer.valueOf(year) - 2018 <= 0) {
            mYearWheelView.setSelection(0);
        } else {
            mYearWheelView.setSelection(Integer.valueOf(year) - 2018);
        }
        mYearWheelView.setmItemList(yearList);
//        mYearWheelView.setBackground(
//                getResources().getDrawable(R.drawable.time_dividing_line, null));
        mYearWheelView.setOnWheelViewListener(new WheelView1.OnWheelViewListener() {
            @Override
            public void onSelected(int selectedIndex, String item) {
                super.onSelected(selectedIndex, item);
                mDayWheelView.setmItemList(ChinaDate.getNormalLunarDays(
                        ChinaDate.getMaxDayOfMonth(Integer.valueOf(month)
                                , Integer.valueOf(item))));
                mDayWheelView.refreshItemView(mDayWheelView.nCurHeight);
                year = item;
                Log.i(TAG, "wheel view==  " + item);
                Log.i(TAG, "wheel selectedIndex==  " + selectedIndex);
                Log.i(TAG, "wheel year==  " + year);
                Log.i(TAG, "wheel month==  " + month);
            }
        });
        //加载月
        mMonthWheelView.setOffset(2);
        mMonthWheelView.setSelection(Integer.valueOf(month));
        mMonthWheelView.setmItemList(monthList);
//        mMonthWheelView.setBackground(
//                getResources().getDrawable(R.drawable.time_dividing_line, null));
        mMonthWheelView.setOnWheelViewListener(new WheelView1.OnWheelViewListener() {
            @Override
            public void onSelected(int selectedIndex, String item) {
                super.onSelected(selectedIndex, item);
                if (item.isEmpty() || "".equals(item)) {
                    return;
                }
                selectedIndex = Integer.valueOf(item);
                mDayWheelView.setmItemList(ChinaDate.getNormalLunarDays(
                        ChinaDate.getMaxDayOfMonth(selectedIndex, Integer.valueOf(year))));
                mDayWheelView.refreshItemView(mDayWheelView.nCurHeight);
                month = item;
                Log.i(TAG, "wheel view==  " + item);
                Log.i(TAG, "wheel selectedIndex==  " + selectedIndex);
                Log.i(TAG, "wheel view max day= " + ChinaDate.getMaxDayOfMonth(selectedIndex, Integer.valueOf(year)));
            }
        });
        //加载日
        mDayWheelView.setOffset(2);
        mDayWheelView.setSelection(Integer.valueOf(day) - 1);
//        mDayWheelView.setBackground(
//                getResources().getDrawable(R.drawable.time_dividing_line, null));
        mDayWheelView.setOnWheelViewListener(new WheelView1.OnWheelViewListener() {
            @Override
            public void onSelected(int selectedIndex, String item) {
                super.onSelected(selectedIndex, item);
                day = item;
                Log.i(TAG, "wheel view==  " + item);
            }
        });
    }

    private void initTime() {
        if (Constants.VALUE_TIME_STYLE12) {
            maxHour = 12;
            mAmPmWheel.setVisibility(View.VISIBLE);
        } else {
            mAmPmWheel.setVisibility(View.GONE);
        }
        if (Calendar.AM == calendar.get(Calendar.AM_PM)) {
            amPm = Calendar.AM;
        } else {
            amPm = Calendar.PM;
        }
        List<String> hourList = new ArrayList<>();
        if (maxHour == 23) {
            for (int i = 0; i <= maxHour; i++) {
                hourList.add(String.valueOf(i));
            }
        } else {
            for (int i = 1; i <= maxHour; i++) {
                hourList.add(String.valueOf(i));
            }
        }
        List<String> minList = new ArrayList<>();
        for (int i = 0; i <= 59; i++) {
            minList.add(String.valueOf(i));
        }
        List<String> amPmList = new ArrayList<>();
        amPmList.add(getString(R.string.morning));
        amPmList.add(getString(R.string.afternoon));
        //加载时
        mHourWheel.setOffset(2);
        if (Constants.VALUE_TIME_STYLE12) {
            mHourWheel.setSelection(calendar.get(Calendar.HOUR) - 1);
        } else {
            mHourWheel.setSelection(calendar.get(Calendar.HOUR_OF_DAY));
        }
        mHourWheel.setmItemList(hourList);
//        mHourWheel.setBackground(
//                getResources().getDrawable(R.drawable.time_dividing_line, null));
        mHourWheel.setOnWheelViewListener(new WheelView1.OnWheelViewListener() {
            @Override
            public void onSelected(int selectedIndex, String item) {
                super.onSelected(selectedIndex, item);
                if (item.isEmpty() || "".equals(item)) {
                    return;
                }
                hour = Integer.valueOf(item);
                Log.i(TAG, "wheel view==  " + item);
                Log.i(TAG, "wheel selectedIndex==  " + selectedIndex);
            }

        });
        //加载分
        mMinWheel.setOffset(2);
        mMinWheel.setSelection(calendar.get(Calendar.MINUTE));
        mMinWheel.setmItemList(minList);
//        mMinWheel.setBackground(
//                getResources().getDrawable(R.drawable.time_dividing_line, null));
        mMinWheel.setOnWheelViewListener(new WheelView1.OnWheelViewListener() {
            @Override
            public void onSelected(int selectedIndex, String item) {
                super.onSelected(selectedIndex, item);
                if (item.isEmpty() || "".equals(item)) {
                    return;
                }
                min = Integer.valueOf(item);
                Log.i(TAG, "wheel view==  " + item);
            }
        });
        //加载上下午
        mAmPmWheel.setOffset(2);
        if (Calendar.AM == amPm) {
            mAmPmWheel.setSelection(0);
        } else {
            mAmPmWheel.setSelection(1);
        }
        mAmPmWheel.setmItemList(amPmList);
//        mAmPmWheel.setBackground(
//                getResources().getDrawable(R.drawable.time_dividing_line, null));
        mAmPmWheel.setOnWheelViewListener(new WheelView1.OnWheelViewListener() {
            @Override
            public void onSelected(int selectedIndex, String item) {
                super.onSelected(selectedIndex, item);
                if (selectedIndex == 1) {
                    amPm = Calendar.AM;
                } else {
                    amPm = Calendar.PM;
                }
                Log.i(TAG, "wheel view==  " + item);
            }
        });
    }
}

ChinaDate工具类

//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

import android.annotation.SuppressLint;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Locale;

public class ChinaDate {
    private static final long[] LUNAR_INFO = new long[]{43344L, 46240L, 47780L, 44368L, 21977L, 19360L, 42416L, 86390L, 21168L, 43312L, 31060L, 27296L, 44368L, 23378L, 19296L, 42726L, 42208L, 53856L, 60005L, 54576L, 23200L, 30371L, 38608L, 19195L, 19152L, 42192L, 118966L, 53840L, 54560L, 56645L, 46496L, 22224L, 21938L, 18864L, 42359L, 42160L, 43600L, 111189L, 27936L, 44448L};
    private static final String[] nStr1 = new String[]{"", "正", "二", "三", "四", "五", "六", "七", "八", "九", "十", "冬", "腊"};
    private static final String[] GAN = new String[]{"甲", "乙", "丙", "丁", "戊", "己", "庚", "辛", "壬", "癸"};
    private static final String[] ZHI = new String[]{"子", "丑", "寅", "卯", "辰", "巳", "午", "未", "申", "酉", "戌", "亥"};
    private static final String[] ANIMALS = new String[]{"鼠", "牛", "虎", "兔", "龙", "蛇", "马", "羊", "猴", "鸡", "狗", "猪"};
    private static SimpleDateFormat sdf = new SimpleDateFormat("yyyy年M月d日 EEEEE", Locale.getDefault());

    public ChinaDate() {
    }

    private static int lYearDays(int y) {
        int sum = 348;

        for(int i = 32768; i > 8; i >>= 1) {
            if ((LUNAR_INFO[y - 1900] & (long)i) != 0L) {
                ++sum;
            }
        }

        return sum + leapDays(y);
    }

    private static int leapDays(int y) {
        if (leapMonth(y) != 0) {
            return (LUNAR_INFO[y - 1900] & 65536L) != 0L ? 30 : 29;
        } else {
            return 0;
        }
    }

    private static int leapMonth(int y) {
        return (int)(LUNAR_INFO[y - 1900] & 15L);
    }

    private static int monthDays(int y, int m) {
        return (LUNAR_INFO[y - 1900] & (long)(65536 >> m)) == 0L ? 29 : 30;
    }

    private static String AnimalsYear(int y) {
        return ANIMALS[(y - 4) % 12];
    }

    private static String cyclicalm(int num) {
        return GAN[num % 10] + ZHI[num % 12];
    }

    private static String cyclical(int y) {
        int num = y - 1900 + 36;
        return cyclicalm(num);
    }

    private static long[] calElement(int y, int m, int d) {
        long[] nongDate = new long[7];
        int i = false;
        int temp = 0;
        int leap = false;
        Date baseDate = (new GregorianCalendar(1900, 0, 31)).getTime();
        Date objDate = (new GregorianCalendar(y, m - 1, d)).getTime();
        long offset = (objDate.getTime() - baseDate.getTime()) / 86400000L;
        nongDate[5] = offset + 40L;
        nongDate[4] = 14L;

        int i;
        for(i = 1900; i < 2100 && offset > 0L; ++i) {
            temp = lYearDays(i);
            offset -= (long)temp;
            nongDate[4] += 12L;
        }

        if (offset < 0L) {
            offset += (long)temp;
            --i;
            nongDate[4] -= 12L;
        }

        nongDate[0] = (long)i;
        nongDate[3] = (long)(i - 1864);
        int leap = leapMonth(i);
        nongDate[6] = 0L;

        int var10002;
        for(i = 1; i < 13 && offset > 0L; ++i) {
            if (leap > 0 && i == leap + 1 && nongDate[6] == 0L) {
                --i;
                nongDate[6] = 1L;
                temp = leapDays((int)nongDate[0]);
            } else {
                temp = monthDays((int)nongDate[0], i);
            }

            if (nongDate[6] == 1L && i == leap + 1) {
                nongDate[6] = 0L;
            }

            offset -= (long)temp;
            if (nongDate[6] == 0L) {
                var10002 = nongDate[4]++;
            }
        }

        if (offset == 0L && leap > 0 && i == leap + 1) {
            if (nongDate[6] == 1L) {
                nongDate[6] = 0L;
            } else {
                nongDate[6] = 1L;
                --i;
                var10002 = nongDate[4]--;
            }
        }

        if (offset < 0L) {
            offset += (long)temp;
            --i;
            var10002 = nongDate[4]--;
        }

        nongDate[1] = (long)i;
        nongDate[2] = offset + 1L;
        return nongDate;
    }

    private static String getChinaDate(int day) {
        String a = "";
        if (day == 10) {
            return "初十";
        } else if (day == 20) {
            return "二十";
        } else if (day == 30) {
            return "三十";
        } else {
            int two = day / 10;
            if (two == 0) {
                a = "初";
            }

            if (two == 1) {
                a = "十";
            }

            if (two == 2) {
                a = "廿";
            }

            if (two == 3) {
                a = "三";
            }

            int one = day % 10;
            switch(one) {
            case 1:
                a = a + "一";
                break;
            case 2:
                a = a + "二";
                break;
            case 3:
                a = a + "三";
                break;
            case 4:
                a = a + "四";
                break;
            case 5:
                a = a + "五";
                break;
            case 6:
                a = a + "六";
                break;
            case 7:
                a = a + "七";
                break;
            case 8:
                a = a + "八";
                break;
            case 9:
                a = a + "九";
            }

            return a;
        }
    }

    public static int getMaxDayOfMonth(int month, int year) {
        if (month != 1 && month != 3 && month != 5 && month != 7 && month != 8 && month != 10 && month != 12) {
            if (month != 4 && month != 6 && month != 9 && month != 11) {
                return year % 4 == 0 ? 29 : 28;
            } else {
                return 30;
            }
        } else {
            return 31;
        }
    }

    public static String getCurrentLunarDate() {
        Calendar today = Calendar.getInstance(Locale.SIMPLIFIED_CHINESE);
        int year = today.get(1);
        int month = today.get(2) + 1;
        int date = today.get(5);
        long[] l = calElement(year, month, date);
        StringBuffer sToday = new StringBuffer();

        String var6;
        try {
            sToday.append(sdf.format(today.getTime()));
            sToday.append(" 农历");
            sToday.append(cyclical(year));
            sToday.append('(');
            sToday.append(AnimalsYear(year));
            sToday.append(")年");
            sToday.append(nStr1[(int)l[1]]);
            sToday.append("月");
            sToday.append(getChinaDate((int)l[2]));
            var6 = sToday.toString();
        } finally {
            sToday = null;
        }

        return var6;
    }

    public static String oneDay(int year, int month, int day) {
        long[] l = calElement(year, month, day);
        StringBuffer sToday = new StringBuffer();

        String var5;
        try {
            sToday.append(" 农历");
            sToday.append(cyclical(year));
            sToday.append('(');
            sToday.append(AnimalsYear(year));
            sToday.append(")年");
            sToday.append(nStr1[(int)l[1]]);
            sToday.append("月");
            sToday.append(getChinaDate((int)l[2]));
            var5 = sToday.toString();
        } finally {
            sToday = null;
        }

        return var5;
    }

    private static String getLunarYearText(int lunarYear) {
        return GAN[(lunarYear - 4) % 10] + ZHI[(lunarYear - 4) % 12] + "年";
    }

    @SuppressLint({"DefaultLocale"})
    public static ArrayList<String> getYears(int startYear, int endYear) {
        ArrayList<String> years = new ArrayList();

        for(int i = startYear; i < endYear; ++i) {
            years.add(String.format("%s(%d)", getLunarYearText(i), i));
        }

        return years;
    }

    public static ArrayList<String> getMonths(int year) {
        ArrayList<String> baseMonths = new ArrayList();

        for(int i = 1; i < nStr1.length; ++i) {
            baseMonths.add(nStr1[i] + "月");
        }

        if (leapMonth(year) != 0) {
            baseMonths.add(leapMonth(year), "闰" + nStr1[leapMonth(year)] + "月");
        }

        return baseMonths;
    }

    public static ArrayList<String> getLunarDays(int maxDay) {
        ArrayList<String> days = new ArrayList();

        for(int i = 1; i <= maxDay; ++i) {
            days.add(getChinaDate(i));
        }

        return days;
    }

    public static ArrayList<String> getNormalLunarDays(int maxDay) {
        ArrayList<String> dayList = new ArrayList();

        for(int a = 1; a <= maxDay; ++a) {
            dayList.add(String.valueOf(a));
        }

        return dayList;
    }
}

你可能感兴趣的:(自定义WheelView实现日期时间选择器)