tl;dr
LocalDate.now( ZoneId.of( "America/Montreal" ) ) // Today’s date.
.plusWeeks( 1 ) // Yields `LocalDate` object
.getDayOfMonth() // Yields `int` number
java.time
Java 8 and later comes with the java.time framework. These new classes supplant the old java.util.Date
/.Calendar
classes. For older Android, see the ThreeTen-Backport and ThreeTenABP projects described below.
These classes include the LocalDate
class for when you want a date-only without time-of-day and without time zone. But note that a time zone is crucial in determining the current date as a new day dawns earlier in the east.
ZoneId zoneId = ZoneId.of( "America/Montreal" );
LocalDate today = LocalDate.now( zoneId );
LocalDate weekLater = today.plusWeeks( 1 ); // Automatically rolls over between months, no problem.
If so desired, you can interrogate that LocalDate
object for its day-of-month number.
int dayOfMonth = weekLater.getDayOfMonth();
About java.time
The java.time framework is built into Java 8 and later. These classes supplant the troublesome old date-time classes such as java.util.Date
, .Calendar
, & java.text.SimpleDateFormat
.
The Joda-Time project, now in maintenance mode, advises migration to java.time.
To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations.
Much of the java.time functionality is back-ported to Java 6 & 7 in ThreeTen-Backport and further adapted to Android in ThreeTenABP (see How to use…).
The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval
, YearWeek
, YearQuarter
, and more.
Joda-Time
UPDATE: The Joda-Time project is now in maintenance mode. The team advises migration to the java.time classes. I leave this section intact as history.
In Android without Java 8 technology, you can add the Joda-Time library to your project. But know that the Joda-Time project is in maintenance mode and advises migration to java.time classes (see ThreeTenABP above for Android).
Joda-Time provided the inspiration for java.time. In this case the code needed is quite similar.
DateTimeZone zone = DateTimeZone.forID( "America/Montreal" );
LocalDate today = LocalDate.now( zone );
LocalDate weekLater = today.plusWeeks( 1 ); // Automatically rolls over between months, no problem.
int dayOfMonth = weekLater.getDayOfMonth();