使用日期轮选择器的令人敬畏的错误?

时间:2014-02-01 05:42:50

标签: android date

日期轮选择器出了问题。它必须更改特定月份的日期。 30,二月没有28天。在31,所有月份变为31.但是另一天,它运作良好。(例如。在模拟器日期是1-2-2014,它运作良好,但昨天,模拟器日期是31-1-2014,所有月份是31 。)另一个问题,在2月30日,2月没有28.我该怎么办,给我一些建议。

    package com.example.aa;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import android.app.Dialog;
import android.content.Context;
import android.graphics.Typeface;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TableLayout.LayoutParams;
import android.widget.TextView;
import kankan.wheel.widget.adapters.ArrayWheelAdapter;
import kankan.wheel.widget.adapters.NumericWheelAdapter;
import kankan.wheel.widget.OnWheelChangedListener;
import kankan.wheel.widget.WheelView;

public class DatePickerDailog extends Dialog {

    private Context Mcontex;

    private int NoOfYear = 113;

    public DatePickerDailog(Context context, Calendar calendar,
            final DatePickerListner dtp) {

        super(context);
        Mcontex = context;
        LinearLayout lytmain = new LinearLayout(Mcontex);
        lytmain.setOrientation(LinearLayout.VERTICAL);
        LinearLayout lytdate = new LinearLayout(Mcontex);
        LinearLayout lytbutton = new LinearLayout(Mcontex);

        Button btnset = new Button(Mcontex);
        Button btncancel = new Button(Mcontex);

        btnset.setText("Set");
        btncancel.setText("Cancel");

        final WheelView month = new WheelView(Mcontex);
        month.setMinimumHeight(300);
        final WheelView year = new WheelView(Mcontex);
        year.setMinimumHeight(300);
        final WheelView day = new WheelView(Mcontex);
        day.setMinimumHeight(300);

        lytdate.addView(day, new LayoutParams(
                android.view.ViewGroup.LayoutParams.WRAP_CONTENT,
                android.view.ViewGroup.LayoutParams.WRAP_CONTENT, 1.2f));
        lytdate.addView(month, new LayoutParams(
                android.view.ViewGroup.LayoutParams.WRAP_CONTENT,
                android.view.ViewGroup.LayoutParams.WRAP_CONTENT, 0.8f));
        lytdate.addView(year, new LayoutParams(
                android.view.ViewGroup.LayoutParams.WRAP_CONTENT,
                android.view.ViewGroup.LayoutParams.WRAP_CONTENT, 1f));
        requestWindowFeature(Window.FEATURE_NO_TITLE);

        lytbutton.addView(btnset, new LayoutParams(
                android.view.ViewGroup.LayoutParams.FILL_PARENT,
                android.view.ViewGroup.LayoutParams.WRAP_CONTENT, 1f));

        lytmain.addView(lytbutton);
        lytmain.addView(lytdate);

        setContentView(lytmain);

        getWindow().setLayout(LayoutParams.FILL_PARENT,
                LayoutParams.WRAP_CONTENT);
        getWindow().setBackgroundDrawableResource(android.R.color.white);

        OnWheelChangedListener listener = new OnWheelChangedListener() {
            public void onChanged(WheelView wheel, int oldValue, int newValue) {

                updateDays(year, month, day);

            }
        };

        // month
        int curMonth = calendar.get(Calendar.MONTH);
        Log.i("curMonth",curMonth+"");
        String months[] = new String[] { "January", "February", "March",
                "April", "May", "June", "July", "August", "September",
                "October", "November", "December" };
        month.setViewAdapter(new DateArrayAdapter(context, months, curMonth));
        month.setCurrentItem(curMonth);
        month.addChangingListener(listener);

        Calendar cal = Calendar.getInstance();

        // year
        int curYear = calendar.get(Calendar.YEAR);
        Log.i("curYear", curYear+"");
        int Year = cal.get(Calendar.YEAR);

        ArrayList<Integer> yearlist = new ArrayList<Integer>();
        int [] yearArray=null;

        for(int i = 1900 ; i <= curYear ; i++){
            yearlist.add(i);
        }
        yearArray = new int[yearlist.size()];

        for(int i = 0 ; i < yearArray.length ;i++){
            yearArray[i] = yearlist.get(i);
        }

        year.setViewAdapter(new DateNumericAdapter(context,1900,curYear, curYear));
        year.setCurrentItem(curYear-1900);
        year.addChangingListener(listener);
        // day
        updateDays(year, month, day);
        day.setCurrentItem(calendar.get(Calendar.DAY_OF_MONTH) - 1);
        Log.i("calendar.get(Calendar.DAY_OF_MONTH) - 1",calendar.get(Calendar.DAY_OF_MONTH) - 1+"");
        btnset.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Calendar c = updateDays(year, month, day);
                dtp.OnDoneButton(DatePickerDailog.this, c);
            }
        });
        btncancel.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                dtp.OnCancelButton(DatePickerDailog.this);

            }
        });
    }

        Calendar updateDays(WheelView year, WheelView month, WheelView day) {

            Calendar calendar = Calendar.getInstance();

            calendar.set(Calendar.YEAR,1900+year.getCurrentItem());

            calendar.set(Calendar.MONTH, month.getCurrentItem());

            Log.i("month.getCurrentItem()",month.getCurrentItem()+"");

            int maxDays = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
            Log.i("MaxDays",maxDays+"");
            day.setViewAdapter(new DateNumericAdapter(Mcontex, 1, maxDays, calendar.get(Calendar.DAY_OF_MONTH) - 1));

            int curDay = Math.min(maxDays, day.getCurrentItem() + 1);

            day.setCurrentItem(curDay - 1, true);
            calendar.set(Calendar.DAY_OF_MONTH, curDay);
            //calendar.set(Calendar.DAY_OF_MONTH, day.getCurrentItem()+1);
            Log.i("Calendar",calendar+"");

            Long milliCalendar = calendar.getTimeInMillis();
            Calendar currentCalendar = Calendar.getInstance();
            Long millicurrentCalendar = currentCalendar.getTimeInMillis();

            if( milliCalendar > millicurrentCalendar){
                return currentCalendar;
            }
            else{
                return calendar;
            }
    }



    private class DateNumericAdapter extends NumericWheelAdapter {
        int currentItem;
        int currentValue;

        public DateNumericAdapter(Context context, int minValue, int maxValue,
                int current) {
            super(context, minValue, maxValue);
            this.currentValue = current;
            setTextSize(20);
        }

        @Override
        protected void configureTextView(TextView view) {
            super.configureTextView(view);

            view.setTypeface(null, Typeface.BOLD);
        }

        @Override
        public View getItem(int index, View cachedView, ViewGroup parent) {
            currentItem = index;
            return super.getItem(index, cachedView, parent);
        }
    }

    private class DateArrayAdapter extends ArrayWheelAdapter<String> {
        int currentItem;
        int currentValue;

        public DateArrayAdapter(Context context, String[] items, int current) {
            super(context, items);
            this.currentValue = current;
            setTextSize(23);
        }

        @Override
        protected void configureTextView(TextView view) {
            super.configureTextView(view);
            if (currentItem == currentValue) {

            }
            view.setTypeface(null, Typeface.BOLD);
        }

        @Override
        public View getItem(int index, View cachedView, ViewGroup parent) {
            currentItem = index;
            return super.getItem(index, cachedView, parent);
        }
    }

    public interface DatePickerListner {
        public void OnDoneButton(Dialog datedialog, Calendar c);

        public void OnCancelButton(Dialog datedialog);
    }
}

1 个答案:

答案 0 :(得分:0)

这里的罪魁祸首是

Calendar cal = Calendar.getInstance();

我使用Joda Time而不是标准java.util.Calendar / date

解决了这个问题
GregorianChronology calendar = GregorianChronology.getInstance();
DateTimeField field = calendar.dayOfMonth();
LocalDate date = new LocalDate(mYear, mMonth+1, 1, calendar);
int max = field.getMaximumValue(date);