1

Does anyone know how to set the day of the week when a date is selected from a date time picker?

I currently have the following to set the date but I also require the day e.g Mon, Tues:

private DatePickerDialog.OnDateSetListener mDateSetListener = new DatePickerDialog.OnDateSetListener() {

        public void onDateSet(DatePicker view, int year, int monthOfYear,
                int dayOfMonth) {
            mYear = year;
            mMonth = monthOfYear;
            mDay = dayOfMonth;
            // calendar.set(mYear, mMonth, mDay);

        }
    };

private void setDateInEditText() {

    StringBuilder dateTime = new StringBuilder (mDay)
            .append("-").append(mMonth + 1).append("-").append(mYear)
            .append(" ").append(pad(mHour)).append(":")
            .append(pad(mMinute));

    datatableEditText.setText("");
    datatableEditText.setText(dateTime);

}

I don't want to set the current day I want to set the day of the date selected.

Many thanks in advance.

Hesham Saeed
  • 5,358
  • 7
  • 37
  • 57
Tuffy G
  • 1,521
  • 8
  • 31
  • 42
  • Possible duplicate of [Android: how to get the current day of the week (Monday, etc...) in the user's language?](http://stackoverflow.com/questions/7651221/android-how-to-get-the-current-day-of-the-week-monday-etc-in-the-users-l) – bummi Jan 28 '16 at 14:57

3 Answers3

2

Checkout this answer on SO https://stackoverflow.com/a/7651306/1321873

SimpleDateFormat sdf = new SimpleDateFormat("EEEE");
Date d = new Date(mYear, mMonth, mDay);
String dayOfTheWeek = sdf.format(d);
Community
  • 1
  • 1
user1417430
  • 376
  • 2
  • 6
  • hi. i've tried that one. it gives me todays day. rather than the date selected – Tuffy G Jun 26 '12 at 09:42
  • Maybe you didn't notice how the date is created. Use the picked year, month and day to create the `Date` object. See my code snippet. – user1417430 Jun 26 '12 at 09:46
1

here is a simple code . you can modify the code as you like to get the correct output

  Calendar cal = new GregorianCalendar(2012, Calendar.JUNE, 26);
  int dayOfWeek = cal.get(Calendar.DAY_OF_WEEK);

The day-of-week is an integer value where 1 is Sunday, 2 is Monday, ..., and 7 is Saturday

user1057197
  • 479
  • 1
  • 6
  • 15
0

You can get the day of the week from the Calendar object. For instance:

Calendar mycal = Calendar.getInstance();
mycal.set(mYear, mMonth, mDay);
int dayOfWeek = mycal.get(Calendar.DAY_OF_WEEK);
String dayString;
switch(dayOfWeek) {
case Calendar.SUNDAY:
    dayString = "Sunday";
    break;
...
}
datatableEditText.setText(dayString);
adam.baker
  • 1,447
  • 1
  • 14
  • 30