27

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".

VaaS
  • 637
  • 2
  • 12
  • 18

9 Answers9

73

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); 
Boris Strandjev
  • 46,145
  • 15
  • 108
  • 135
11

Just use a single line of code:

android.text.format.DateFormat.format("EEEE", date);
Zero Piraeus
  • 56,143
  • 27
  • 150
  • 160
  • 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
11

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)];
Vivek Khandelwal
  • 7,829
  • 3
  • 25
  • 40
  • 2
    String[] days = new String[] {"", "SUNDAY", "MONDAY", "TUESDAY", "WEDNESDAY", "THURSDAY", "FRIDAY", "SATURDAY" } – Milon May 01 '16 at 11:32
  • 2
    String 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
3
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/

dandan78
  • 13,328
  • 13
  • 64
  • 78
Sandeep Tiwari
  • 2,042
  • 3
  • 24
  • 47
0

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];
Domysee
  • 12,718
  • 10
  • 53
  • 84
0

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;
}
Juboraj Sarker
  • 947
  • 1
  • 9
  • 13
0
Calendar date = Calendar.getInstance();
String dayToday = android.text.format.DateFormat.format("EEEE", date).toString();
TextView myTextView = findViewById(R.id.myTextView);
myTextView.setText(dayToday);
Simas Joneliunas
  • 2,890
  • 20
  • 28
  • 35
Sarimh9
  • 1
  • 1
  • 1
    While 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
0

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.

Saad Farooq
  • 13,172
  • 10
  • 68
  • 94
-1

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.

Bharath Kumar
  • 541
  • 3
  • 10