Jennifer
Jennifer

Reputation: 1852

How to get the day like "Monday" string from "2015-02-24" in android

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);

Upvotes: 1

Views: 4095

Answers (5)

Basil Bourque
Basil Bourque

Reputation: 340118

tl;dr

Use java.time classes LocalDate and DayOfWeek.

LocalDate
        .parse ( "2015-02-24" )
        .getDayOfWeek ( )
        .getDisplayName (
                TextStyle.FULL_STANDALONE ,
                Locale.of ( "en" , "US" )
        )

Tuesday

java.time

Android now offers the java.time functionality found in Java 8+.

  • Android 26+ offers the classes.
  • For earlier Android, the latest tooling provides most of the java.time functionality via “API desugaring˜”.

Parse your input as a LocalDate.

LocalDate ld = LocalDate.parse( "2015-02-24" ) ;

Extract the day of week.

DayOfWeek dow = ld.getDayOfWeek();

Ask the DayOfWeek object to generate a localized string for the name of that day.

Locale locale = new Locale ( "fr" , "CA" );  // French language, Canada cultural norms. In modern Java, use `Locale.of` instead of `new`.
String output = dow.getDisplayName ( TextStyle.FULL_STANDALONE , locale );

mardi

Or use Locale.of ( "en" , "US" ).

Tuesday

Upvotes: 1

Bugs Happen
Bugs Happen

Reputation: 2268

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.

Upvotes: 0

kemdo
kemdo

Reputation: 1459

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.

Upvotes: 3

Yogendra
Yogendra

Reputation: 5288

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();
        }

Upvotes: 4

zorbathegeek
zorbathegeek

Reputation: 88

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);

Upvotes: 1

Related Questions