Reputation: 3982
I am using Joda time and i have a problem to set day of the week and week of the month. Suppose if i select April 23,2013
which is (3rd day of 4th week of the month) i want next date 3rd day of 4th week of the month
which is May 21. Is there a way to do this?
Upvotes: 2
Views: 5450
Reputation: 699
I dont recomended the uses of calendar api because it is very complicated... isn't simplest... as Ilya said.
The very simples is jodatime api. you can use those many methods of joda api for get many results of simplest form:
Then you can do this:
Returns the third day of week of current date:
Datetime dateTime = DateTime.now().withDayOfWeek(3)
Set the especific month, in this case April:
dateTime = dateTime.withMonthOfYear(4)
I dont know if jodatime has the method for get week of the month, but you can search better in the jodatime api doc Exists the methods:
.withWeekOfWeekyear(int)
and
.withWeekyear(int)
At method that starts with word : .with... (Ex.: dateTime.withHourOfDay(2)) you can get many results. Look there and have fun...
Abs!
Upvotes: 3
Reputation: 29693
The simplest way to do it is next : use class java.util.Calendar
static DateTime nextMonth(final DateTime current)
{
final Calendar curr = current.toCalendar(Locale.getDefault());
final Calendar retVal = Calendar.getInstance();
retVal.set(Calendar.DAY_OF_WEEK, curr.get(Calendar.DAY_OF_WEEK));
retVal.set(Calendar.WEEK_OF_MONTH, curr.get(Calendar.WEEK_OF_MONTH));
retVal.set(Calendar.MONTH, curr.get(Calendar.MONTH) + 1);
return new DateTime(retVal).withMillisOfDay(0);
}
Usage
DateTime dateTime = new DateTime(2013, 04, 23, 0 ,0 ,0 ,0); //April 23,2013
DateTime nextMonth = nextMonth(dateTime); // May 21, 2013
Upvotes: -2