user1390816
user1390816

Reputation: 623

How to add one month to a date and get the same day

I try to add one month to a date depending on weekdays. Fore example the date is the 3. Monday of September. After adding I want have the 3. Monday of October. I tried to add one month to following date

Mon Sep 17 17:30:00 MESZ 2012

with this code:

Calendar date = Calendar.getInstance();
date.setTimeInMillis(date_in_mil);
date.add(Calendar.DAY_OF_WEEK_IN_MONTH, 3); 

But I got

Mon Oct 08 17:30:00 MESZ 2012

which is the second Monday of Oct and not the third. Has anybody an idea how this workes?

EDIT This is the solution I used like in the answer below:

int prevDayOfWeek = date.get(Calendar.DAY_OF_WEEK);
date.add(Calendar.MONTH, 1);
date.set(Calendar.DAY_OF_WEEK, prevDayOfWeek);
date.set(Calendar.DAY_OF_WEEK_IN_MONTH, week);

wereby week is the number of the week in a month. Fore example 1 means the first, 2 the second, and so on. But week can also count backwards, fore example -1 means the last week of month.

Upvotes: 6

Views: 29182

Answers (6)

user7761750
user7761750

Reputation: 1

The solution with a String date dd/mm/yyyy:

String mes = null;
String yourfecha = null;
if(yourfecha.substring(5,6).equals("/")){
mes = yourfecha.substring(3,5)
}else{
mes = yourfecha.substring(4,6)
}
Integer newmes = Integer.valueOf(mes)+{How many months want to add};
String anyo = yourfecha.substring(6,10);
String newFecha=null;
if(newmes >12){            
newmes=newmes-12;
Integer newanyo=Integer.valueOf(anyo)+1;
newFecha=yourfecha.substring(0,2)+"/0"+newmes + "/"+newanyo;       
}else{
newFecha=yourfecha.substring(0,4)+newmes+"/"+anyo;  
}   
return newFecha;

Upvotes: -1

Yogendra Singh
Yogendra Singh

Reputation: 34397

If you want to add a month, do the below:

Calendar date = Calendar.getInstance();
date.setTimeInMillis(date_in_mil);
date.add(Calendar.MONTH, 1);

if you want to add 4 weeks,

Calendar date = Calendar.getInstance();
date.setTimeInMillis(date_in_mil);
date.add(Calendar.DAY_OF_WEEK_IN_MONTH, 4);

To keep the week always as third week:

if(date.get(Calendar.WEEK_OF_MONTH)<3){
   date.add(Calendar.DAY_OF_WEEK_IN_MONTH, 1);
 }

or better do like

date.add(Calendar.DAY_OF_WEEK_IN_MONTH, 3-date.get(Calendar.WEEK_OF_MONTH);

Upvotes: 1

Ilya
Ilya

Reputation: 29741

If you want get 3rd monday of month, then use set instead of add
date.set(Calendar.DAY_OF_WEEK_IN_MONTH, 3);

if you want add one month to current date, use
date.add(Calendar.MONTH, 1);

EDIT

final Calendar date = Calendar.getInstance();
date.set(2012, Calendar.SEPTEMBER, 17);

int prevDayOfWeekInMonth = date.get(Calendar.DAY_OF_WEEK_IN_MONTH);
int prevDayOfWeek = date.get(Calendar.DAY_OF_WEEK);

date.add(Calendar.MONTH, 1);
date.set(Calendar.DAY_OF_WEEK, prevDayOfWeek);
date.set(Calendar.DAY_OF_WEEK_IN_MONTH, prevDayOfWeekInMonth);

Upvotes: 12

saum22
saum22

Reputation: 882

Calendar c= Calendar.getInstance();
System.out.println(c.get(Calendar.DATE));//if this is 2nd monday of September
System.out.println(c.WEEK_OF_MONTH);
c.add(c.WEEK_OF_MONTH, 4);
System.out.println(c.get(Calendar.DATE));//prints the 2 monday of october

Upvotes: 0

Kumar Vivek Mitra
Kumar Vivek Mitra

Reputation: 33544

Use Calendar class's roll() method, unlike add() method of the Calendar class it will only modify the part of the date which is mentioned.

Eg:

public class Cal {

    public static void main(String[] args){

        Calendar c = Calendar.getInstance();

        c.roll(Calendar.MONTH, 1);

        System.out.println(c.getTime());
    }

}

Upvotes: 1

Pramod
Pramod

Reputation: 417

http://docs.oracle.com/javase/1.4.2/docs/api/java/util/Calendar.html

Inconsistent information. If fields conflict, the calendar will give preference to fields set more recently. For example, when determining the day, the calendar will look for one of the following combinations of fields. The most recent combination, as determined by the most recently set single field, will be used.

 MONTH + DAY_OF_MONTH
 MONTH + WEEK_OF_MONTH + DAY_OF_WEEK
 MONTH + DAY_OF_WEEK_IN_MONTH + DAY_OF_WEEK
 DAY_OF_YEAR
 DAY_OF_WEEK + WEEK_OF_YEAR

Upvotes: 0

Related Questions