1

I have a string "2015-02-24", how can I get "day name like Monday or Mon" in android. I try to use the code below but failed.

SimpleDateFormat sdf = new SimpleDateFormat("EEEE");
                    Date d = new Date("2015-02-24");
String dayOfTheWeek = sdf.format(d);
Jennifer
  • 1,822
  • 2
  • 20
  • 45
  • 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) – Fahim Mar 30 '15 at 07:51

4 Answers4

4
String input_date_string="2015-02-24";
        SimpleDateFormat dateformat=new SimpleDateFormat("yyyy-MM-dd");
        Date date;
        try {
            date = dateformat.parse(input_date_string);
            DateFormat dayFormate=new SimpleDateFormat("EEEE"); 
            String dayFromDate=dayFormate.format(date);
            Log.d("asd", "----------:: "+dayFromDate);

        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
Yogendra
  • 4,817
  • 1
  • 28
  • 21
3

Use Calender instance or use DateFormat

Simple :

Calendar c = Calendar.getInstance();
c.setTime(yourDate);
int dayOfWeek = c.get(Calendar.DAY_OF_WEEK);

Or:

String dayOfTheWeek = (String) android.text.format.DateFormat.format("EEEE", date);//Thursday
String stringMonth = (String) android.text.format.DateFormat.format("MMM", date); //Jun
String intMonth = (String) android.text.format.DateFormat.format("MM", date); //06
String year = (String) android.text.format.DateFormat.format("yyyy", date); //2013
String day = (String) android.text.format.DateFormat.format("dd", date); //20

UPDATE: you have to see Date in Java doc:

Constructor Summary:

Date(): Allocates a Date object and initializes it so that it represents the time at which it was allocated, measured to the nearest millisecond.

Date(int year, int month, int date): Deprecated. As of JDK version 1.1, replaced by Calendar.set(year + 1900, month, date) or GregorianCalendar(year + 1900, month, date).

Date(int year, int month, int date, int hrs, int min): Deprecated. As of JDK version 1.1, replaced by Calendar.set(year + 1900, month, date, hrs, min) or GregorianCalendar(year + 1900, month, date, hrs, min).

Date(int year, int month, int date, int hrs, int min, int sec): Deprecated. As of JDK version 1.1, replaced by Calendar.set(year + 1900, month, date, hrs, min, sec) or GregorianCalendar(year + 1900, month, date, hrs, min, sec).

Date(long date): Allocates a Date object and initializes it to represent the specified number of milliseconds since the standard base time known as "the epoch", namely January 1, 1970, 00:00:00 GMT.

Date(String s): Deprecated. As of JDK version 1.1, replaced by DateFormat.parse(String s).

Only Date() and Date(long Date) still usable.

Community
  • 1
  • 1
kemdo
  • 1,429
  • 3
  • 15
  • 29
1

You need to first convert the date in String format to a Date object. This can be done via:

SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
Date date = dateFormat.parse(date);

You can then format the above date via:

SimpleDateFormat weekDayFormat = new SimpleDateFormat("EEEE");
String dayOfTheWeek = weekDayFormat.format(date);
0

All the answers are correct. I am just summing up the essence of all.

First you need to parse your String into Date, like this:

SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
try {
    Date date = dateFormat.parse("2015-02-24");
}
catch (ParseException e) {
    e.printStackTrace();
}

Your date is ready :)

But major functions of Date class are deprecated. So you should use Calendar instead. Like this:

Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK);
switch(dayOfWeek)
{
    case Calendar.SUNDAY: 
        break;
    case Calendar.MONDAY:
        break;
    case Calendar.TUESDAY:
        break;
    case Calendar.WEDNESDAY:
        break;
    case Calendar.THURSDAY:
        break;
    case Calendar.FRIDAY:
        break;
    case Calendar.SATURDAY:
        break;
}

Now with calendar, you can get any thing you want. Here is more information for Calendar on Android Docs.

Bugs Happen
  • 2,169
  • 4
  • 33
  • 59
  • I'm trying your sample and I have one problem. I've inserted "2015-03-17" and the date result is Tue Mar, however, in the switch statement I get the 7 = Sat. Actually I should get the 3 = TUESDAY. – Jennifer Mar 30 '15 at 09:28
  • 1
    please see the edited code. I have used `calendar.get()` method now and it is working fine. Thanks for this edit :) – Bugs Happen Mar 30 '15 at 09:41