How to add 7 days to current date while not going over available days of a month?

33,051

Solution 1

add(Calendar.DAY_OF_MONTH, 7);

From Calendar JavaDoc

Solution 2

Calendar's add method does this for you: cal.add(Calendar.DATE, 7);

EDIT: Given the extended comments, I guess I should add to this by saying that if cal begins as October 4, 2011, and I call cal.add(Calendar.DATE, 7) the new value of cal is October 11, 2011. Similarly, if cal begins as March 29, 2025, then after cal.add(Calendar.DATE, 7) the new value of cal is April 5, 2025.

Solution 3

You have to use the add method of calendar.

cal.add(Calendar.DAY_OF_MONTH, 7);

Solution 4

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

Solution 5

cal.add(Calendar.DAY_OF_MONTH, 7);

Share:
33,051
user856377
Author by

user856377

Updated on November 10, 2020

Comments

  • user856377
    user856377 over 3 years

    I am trying to get todays day of the month.

    And what i want to do is add seven days to the number and the get that current day of the month.

    Also i want it to be able to go to the next month..Lets say today is the 29th. When it adds 7 days how can i get it to go the the next month such as 29 + 7 would equal the 5th of the next month.

    How would i go about doing this?

    Ive already managed to get the current date.

    Calendar cal = Calendar.getInstance();
        int day = cal.get(Calendar.DAY_OF_MONTH);
        int dayOfMonth = day;
        String today = getToday();
    

    I am using this because i would like to launch an asynctask in my main activity every 7 days.