在某些Android设备中,CalenderPickerView显示任何日期的错误日期

时间:2018-10-01 06:08:03

标签: android calendar

我遇到的问题是,Android设备在CalenderPickerView中的任何日期显示错误的日期。我在Mi note 5 pro和Samsung Galaxy A5中遇到了这个问题。对于所有Mi note 5 pro或Samsung galaxy A5而言,这并不是所有情况,在某些设备中会发生。我附上了屏幕截图。请帮助我了解问题以及如何解决。以下是我使用过的日历的代码

    Calendar c = Calendar.getInstance();
    Calendar nextYear = Calendar.getInstance();
    nextYear.add(Calendar.YEAR, 1);
    Date today = new Date();
    today= c.getTime();
    date1 = today;
    Calendar c = Calendar.getInstance();
    c.setTime(date2);
    c.add(Calendar.DATE, 2);
    date2 = c.getTime();
    DateFormat format = new SimpleDateFormat("EEE MMM dd HH:mm:ss Z yyyy", Locale.US);

    try {


        depature_date_text.setText(date_display.format(date1));
        if(Constants.MODE==2) {


            return_date_text.setText(date_display.format(date2));
        }
    } catch (ParseException e) {
        e.printStackTrace();
    }
    //add one year to calendar from todays date
    if (mode==2) {

        calendar_view.init(today, nextYear.getTime())
                .inMode(CalendarPickerView.SelectionMode.RANGE);
    } else {
        //rel_return.setVisibility(View.GONE);
        //return_highlight.setVisibility(View.GONE);
        rel_return.setEnabled(false);
        calendar_view.init(today, nextYear.getTime())
                .inMode(CalendarPickerView.SelectionMode.SINGLE);
    }

enter image description here

1 个答案:

答案 0 :(得分:0)

这可能是因为,当您调用Calendar时,它将以0到6或某些手机1到7的整数形式返回星期几。 要摆脱这种情况,您可能需要添加额外的代码

    Calendar calendar = Calendar.getInstance();
    String i="";
    int dayofWeek = calendar.get(Calendar.DAY_OF_WEEK); 
                    if (dayOfWeek == 1) {
                        i = "Sunday";
                    } else if (dayOfWeek == 2) {
                        i = "Monday";
                    } else if (dayOfWeek == 3) {
                        i = "Tuesday";
                    } else if (dayOfWeek == 4) {
                        i = "Wednesday";
                    } else if (dayOfWeek == 5) {
                        i = "Thursday";
                    } else if (dayOfWeek == 6) {
                        i = "Friday";
                    } else if (dayOfWeek == 7) {
                        i = "Saturday";
                    }

这将完美地在所有设备中恢复一天。 将i放置在您想要的任何地方