Question

i have use the following code to get day_of_week from selected day, but it is working for 1st 7 days only, can anyone please help me.

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];
    }
Was it helpful?

Solution

If you want to display current date from a date picker, you can do it like so:

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()));

        }
};

OTHER TIPS

Replace your method with this.

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);


    }
};

Play with these Symbols for desired output I hope this would help you.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top