-1

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];
    }
Pearl
  • 121
  • 1
  • 8

2 Answers2

0

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

        }
};
nikis
  • 11,166
  • 2
  • 35
  • 45
  • editday.setText(new SimpleDateFormat("dd-MM-yyyy").format(calendar)); this line shows java.lang.IllegalArgumentException at logcat – Pearl Feb 11 '14 at 12:14
  • sorry, i need day of picked date only, day means sunday, monday – Pearl Feb 11 '14 at 12:39
  • ok, please try the following format "EEEE-MM-yyyy" (I haven't tested it, but it should be working per http://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html) – nikis Feb 11 '14 at 12:41
  • i have tried it, it shows Friday-02-2014, how to get day from this, how to seperate it, thanks for this solution, but i need day only – Pearl Feb 11 '14 at 12:44
  • 2
    Like so: String day = new SimpleDateFormat("EEEE").format(calendar.getTime()) – nikis Feb 11 '14 at 12:48
0

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.

Akbarsha
  • 2,422
  • 23
  • 34
  • String formatted_date = (DateFormat.format("MM:d:yyyy", time_val)) .toString(); in this line format shows The method format(Date) in the type DateFormat is not applicable for the arguments (String, long) – Pearl Feb 11 '14 at 12:02
  • Use calSet instead of time_val – Akbarsha Feb 11 '14 at 13:17