15

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.

user856377
  • 281
  • 1
  • 3
  • 11

8 Answers8

31
add(Calendar.DAY_OF_MONTH, 7);

From Calendar JavaDoc

Guvante
  • 18,775
  • 1
  • 33
  • 64
  • How does this add 7 days to the current date? Would it just change the day of the month to the 7th? This is what it looks like in the docs. – user856377 Oct 04 '11 at 22:06
  • 1
    @user856377: There are two methods, set and add. Set does what you describe while add modifies the existing information based on the input. Think of a calendar object as a finger pointing at a date, you are saying "Add seven days to this spot" and the system figures out the details. – Guvante Oct 04 '11 at 22:21
  • Soo...your code sets or 7 days from today? This is what i want to do..All of the answers here are confusing. Could you just put how it would look from my scenario? or at least an example? – user856377 Oct 04 '11 at 22:29
  • write some unit tests to cover the various scenarios and you will understand it better – willCode4Beer Oct 04 '11 at 22:38
  • @user856377: `Calendar cal = Calendar.getInstance();` gives you a `Calendar` instance whose value is today (well technically now). `cal.add(Calendar.DAY_OF_MONTH, 7);` modifies that instance to point to a week from when it originally pointed to, in this case a week from today. You can then use the normal output method like `get(Calendar.DAY_OF_MONTH)` to get the value back out of the object. – Guvante Oct 04 '11 at 22:43
  • Thank You! That was all i needed it – user856377 Oct 04 '11 at 23:05
8

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.

cobaltduck
  • 1,598
  • 5
  • 25
  • 51
  • Could you provide how this would look given the scenario i gave above? – user856377 Oct 04 '11 at 21:22
  • As another commented, you have five nearly identical answers in 30 seconds. I'd follow the link to the Calendar API in Guvante's answer for more. – cobaltduck Oct 04 '11 at 21:26
  • 1
    Notice that the add method takes two parameters. The first param is which field of the Calendar object will be affected, the second is by how much. So the code each answerer has provided will advance the date by 7, and this new value will be in the `cal` variable. Again, really recommended reading the API. – cobaltduck Oct 04 '11 at 21:33
  • Okay so... Im still confused i read over the docs.. It says when you use Calendar.add(). You change the current month,date or day from its current to the int you supply...So how does your answer add 7 days to todays current date? – user856377 Oct 04 '11 at 21:55
  • Wouldnt your answer just change the date to the 7th of the current month> – user856377 Oct 04 '11 at 21:55
7

You have to use the add method of calendar.

cal.add(Calendar.DAY_OF_MONTH, 7);
Tarcísio Júnior
  • 1,239
  • 7
  • 15
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();
Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
4

cal.add(Calendar.DAY_OF_MONTH, 7);

Mark B
  • 183,023
  • 24
  • 297
  • 295
2
    Date m = new Date();
    Calendar cal = Calendar.getInstance();  
    cal.setTime(m);  
    cal.add(Calendar.DATE, 10); // 10 is the days you want to add or subtract   
    m = cal.getTime();   
    System.out.println(m);
umar
  • 21
  • 2
2

Use GregorianCalendar.add(Calendar.DATE, 7). The GregorianCalendar class will take care of rolling the date into the next month.

Jay
  • 26,876
  • 10
  • 61
  • 112
  • So how would this look for examply with code i have above... I get todays date...Then how would i add 7 days to that and get the next date with the calendar.add(Calendar.DATE,) youve given? – user856377 Oct 04 '11 at 21:28
  • The Calendar.add functions move the date forward (or back, if you give a negative number) from the current value of the date. So add(Calendar.DATE, 7) adds 7 days to the current value. add(Calendar.MONTH, 7) would add 7 months, keeping the same day of the month. (With some rules about what it does if that's not a valid date. Like if you add 1 month to August 31, that would give September 31, which is not valid, so it makes it September 30.) – Jay Oct 05 '11 at 15:10
0
Date newDate = new Date(current timestamp in millis + 604800000L);//after 7 days
Makvin
  • 3,475
  • 27
  • 26
  • 1
    A very poor answer. (1) Code only answers are not helpful; you need to explain how this answers the question so we all may learn. (2) The answer is incomplete in that you don’t tell where to get current timestamp in millis from. (3) The code snippets in the other 7 answers are more readable than your code. (4) While using the `Date` class was fine in 2011, when the question was asked, in 2017 I recommend avoiding it and using the modern Java date and time API known as `java.time`. – Ole V.V. Nov 09 '17 at 11:33