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
开发者_如何学Pythonmethod returns the day of the week.
Is there a way to get the day of the month?
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);
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.
This is what I did:
Calendar calendar = Calendar.getInstance();
day = calendar.get(Calendar.DAY_OF_MONTH);
Calling getDate() on your date object will return the day of the month.
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());
}
精彩评论