I have date string 18-2-2012 , from this how to get the current day name i.e., today is "saturday" like this. for tomorrow's date 19-2-2012 and the day name is "sunday".
9 Answers
Use java date formats.
SimpleDateFormat inFormat = new SimpleDateFormat("dd-MM-yyyy");
Date date = inFormat.parse(input);
SimpleDateFormat outFormat = new SimpleDateFormat("EEEE");
String goal = outFormat.format(date);

- 46,145
- 15
- 108
- 135
Just use a single line of code:
android.text.format.DateFormat.format("EEEE", date);

- 56,143
- 27
- 150
- 160

- 129
- 1
- 3
-
This is the best approach in my opinion. Does exactly what the author of the question asked for and is a single line. – Forke Nov 15 '16 at 18:10
You can use Calendar
Calendar calendar = Calendar.getInstance();
calendar.setTime(date_your_want_to_know);
String[] days = new String[] { "SUNDAY", "MONDAY", "TUESDAY", "WEDNESDAY", "THURSDAY", "FRIDAY", "SATURDAY" }
String day = days[calendar.get(Calendar.DAY_OF_WEEK)];

- 7,829
- 3
- 25
- 40
-
2String[] days = new String[] {"", "SUNDAY", "MONDAY", "TUESDAY", "WEDNESDAY", "THURSDAY", "FRIDAY", "SATURDAY" } – Milon May 01 '16 at 11:32
-
2String day = days[calendar.get(Calendar.DAY_OF_WEEK)]; we should chage above line to String day = days[calendar.get(Calendar.DAY_OF_WEEK) - 1]; //because array of index start from 0 – Dinesh Sarma May 31 '21 at 15:18
-
int dayOfWeek=bday.get(Calendar.DAY_OF_WEEK); // Returns 3, for Tuesday!
for more detail go here.... http://mobile.tutsplus.com/tutorials/android/java_android_date-and-time/

- 13,328
- 13
- 64
- 78

- 2,042
- 3
- 24
- 47
On @vivek's answer add "-1" at calendar.get(Calendar.DAY_OF_WEEK)
, since it returns numbers from 1 to 7 but the String array is indexed from 0 to 6.
calendar.setTime(date_your_want_to_know);
String[] days = new String[] { "SUNDAY", "MONDAY", "TUESDAY", "WEDNESDAY", "THURSDAY", "FRIDAY", "SATURDAY" }
String day = days[calendar.get(Calendar.DAY_OF_WEEK)-1];

- 12,718
- 10
- 53
- 84
Try this code. Hope it will work. This is worked for me.
public String daysNameOfWeek(String inputDate){
DateFormat df = DateFormat.getDateInstance(DateFormat.MEDIUM, Locale.UK);
try {
Date date = df.parse(inputDate.trim());
SimpleDateFormat outFormat = new SimpleDateFormat("EEEE");
daysName = outFormat.format(date);
} catch (ParseException e) {
e.printStackTrace();
}
return daysName;
}

- 947
- 1
- 9
- 13
Calendar date = Calendar.getInstance();
String dayToday = android.text.format.DateFormat.format("EEEE", date).toString();
TextView myTextView = findViewById(R.id.myTextView);
myTextView.setText(dayToday);

- 2,890
- 20
- 28
- 35

- 1
- 1
-
1While this piece of code may provide a solution to the question, it's better to add context as to why/how it works. This can help future users refer to and eventually apply this knowledge to their own code. You are also likely to have positive feedback/upvotes from users, when the code is explained. – Amit Verma Mar 03 '21 at 07:34
First convert the date string to a Date using SimpleDateFormat
.
Then make a Calendar
instance from that date.
Finally, retrieve the day of week from the Calendar
using the get(Calendar.DAY_OF_WEEK)
. This will give you an integer between 1 to 7 representing the day of the week. You can simple map this to an array of Strings to get the days.

- 13,172
- 10
- 68
- 94
Below method will return the day by passing the date in String format. null is returned if exception occurs.
private String getDay(String date_in_string_format){
DateFormat df = DateFormat.getDateInstance();
Date date;
try {
date = df.parse(date_in_string_format);
} catch (Exception e) {
Log.e("Error:","Exception " + e);
return null;
}
return new SimpleDateFormat("EEEE").format(date);
}
Hope it helps.

- 541
- 3
- 10