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

I am trying to add one month to the date depending on weekdays. For example, the date is 3. Monday of September. After adding, I want to have 3. Monday of October. I tried to add a month before the next date.

Mon Sep 17 17:30:00 MESZ 2012 

using 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 October, not the third. Does anyone know how this works?

EDIT This is the solution I used, as 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); 

there was a week - this is the number of the week per month. Example 1 - first, second, etc. But a week can also take into account the other side, for example, example -1 means the last week of the month.

+6
source share
6 answers

If you want to get the third Monday of the month, use set instead of add
date.set(Calendar.DAY_OF_WEEK_IN_MONTH, 3);

if you want to add one month to the 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); 
+12
source

If you want to add a month, follow these steps:

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

So that the week is always in the third week:

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

or better to do

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

Use the Calendar class class roll() method, unlike the add() method of the Calendar class method, it will only change part of the specified date.

For instance:

 public class Cal { public static void main(String[] args){ Calendar c = Calendar.getInstance(); c.roll(Calendar.MONTH, 1); System.out.println(c.getTime()); } } 
+1
source
 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 
0
source

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

Inconsistent information. If there are field conflicts, the calendar will give preference to previously set fields. For example, when determining the day, the calendar will look for one of the following combinations of fields. The last combination will be used, determined by the most recently set single field.

  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 
0
source

Solution with date dd / mm / yyyy line:

 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; 
-1
source

Source: https://habr.com/ru/post/927763/


All Articles