Ted pottel
Ted pottel

Reputation: 6983

Is there a way to get the day of month on Android?

I have a Date value and would like to display the day of the month. It seems like the getDays method returns the day of the week.

Is there a way to get the day of the month?

Upvotes: 3

Views: 17437

Answers (5)

Steve Moretz
Steve Moretz

Reputation: 3138

Just use my method if you have unixTime:

public int getDayInt(long unixTime) {
    return Integer.parseInt(DateFormat.format("dd", new Date(unixTime)).toString());
}

or if you have a Date:

public int getDayInt(Date date) {
    return Integer.parseInt(DateFormat.format("dd", date).toString());
}

Upvotes: 0

codeye
codeye

Reputation: 594

Calendar cal = Calendar.getInstance();
cal.setTime(yourDateObj);
int day = cal.get(Calendar.DAY_OF_MONTH);

OR

DateFormat dateFormat = new SimpleDateFormat("dd");
String day = dateFormat.format(yourDateObj);

Upvotes: 6

Nizar Mneimneh
Nizar Mneimneh

Reputation: 41

This is what I did:

    Calendar calendar = Calendar.getInstance();
    day = calendar.get(Calendar.DAY_OF_MONTH);

Upvotes: 4

CRM
CRM

Reputation: 4635

First of all, according to Android Developers website:

This method (getDay()) is deprecated. use `Calendar.get(Calendar.DAY_OF_WEEK)`

Regarding your specific question, the Calendar object has a constant named DAY_OF_WEEK_IN_MONTH which indicates the ordinal number of the day of the week within the current month.

Upvotes: 4

Jayy
Jayy

Reputation: 14728

Calling getDate() on your date object will return the day of the month.

Upvotes: 2

Related Questions