Android:从选定日期获取星期几

时间:2014-02-11 10:14:51

标签: android datepicker dayofweek

我使用以下代码从选定的日期获取day_of_week,但它仅在前7天工作,任何人都可以帮助我。

public DatePickerDialog.OnDateSetListener pickerListener = new DatePickerDialog.OnDateSetListener() {

        @Override
        public void onDateSet(DatePicker view, int selectedYear,
                int selectedMonth, int selectedDay) {

            picyear = selectedYear;
            picmonth = selectedMonth;
            picday = selectedDay;

            picdayday = (getWeek(picday - 1));

            date.setText(new StringBuilder().append(picmonth + 1).append("-")
                    .append(picday).append("-").append(picyear).append(" "));
            editday.setText(picdayday);


        }
    };

public String getWeek(int weekno) {
        return new DateFormatSymbols().getWeekdays()[weekno];
    }

2 个答案:

答案 0 :(得分:0)

如果要显示日期选择器的当前日期,可以这样做:

public DatePickerDialog.OnDateSetListener pickerListener = new DatePickerDialog.OnDateSetListener() {

        @Override
        public void onDateSet(DatePicker view, int selectedYear,
                              int selectedMonth, int selectedDay) {

            Calendar calendar = Calendar.getInstance();
            calendar.set(selectedYear,selectedMonth,selectedDay);
            editday.setText(new SimpleDateFormat("dd-MM-yyyy").format(calendar.getTime()));

        }
};

答案 1 :(得分:0)

用此替换您的方法。

public DatePickerDialog.OnDateSetListener pickerListener = new DatePickerDialog.OnDateSetListener() {

    @Override
    public void onDateSet(DatePicker view, int selectedYear,
            int selectedMonth, int selectedDay) {

        Calendar calNow = Calendar.getInstance();
        Calendar calSet = (Calendar) calNow.clone();

        calSet.set(Calendar.DATE, selectedDay);
        calSet.set(Calendar.MONTH, selectedMonth);
        calSet.set(Calendar.YEAR, selectedYear);

        long time_val = calSet.getTimeInMillis();

        String formatted_date = (DateFormat.format("MM:d:yyyy", time_val))
                .toString();

        date.setText(formatted_date);


    }
};

使用这些Symbols播放所需的输出 我希望这会对你有所帮助。