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);
- 45,192
- 14
- 103
- 128
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,749
- 3
- 24
- 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
-
SUNDAY is not always the first day – Nininea May 24 '22 at 08:59
Just use a single line of code:
android.text.format.DateFormat.format("EEEE", date);
- 52,181
- 26
- 146
- 158
- 109
- 1
- 2
-
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
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/
- 12,893
- 12
- 63
- 75
- 2,002
- 3
- 22
- 44
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,310
- 10
- 51
- 80
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;
}
- 888
- 1
- 8
- 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,705
- 15
- 25
- 32
- 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.
- 12,941
- 9
- 66
- 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.
- 531
- 3
- 10