Possible Duplicate:
How to add days to a date in Java
Consider the date to be 19/05/2013 and the number to be 14. I would like to get the resulting date after adding the number to the month.
Expected result is: 19/07/2014.
Possible Duplicate:
How to add days to a date in Java
Consider the date to be 19/05/2013 and the number to be 14. I would like to get the resulting date after adding the number to the month.
Expected result is: 19/07/2014.
In .NET you could do use the AddMonths
method:
DateTime date = new DateTime(2013, 5, 19);
DateTime newDate = date.AddMonths(14);
As far as parsing a date from a string using a specified format you could use the TryParseExact
method:
string dateStr = "19/05/2013";
DateTime date;
if (DateTime.TryParseExact(dateStr, "dd/MM/yyyy", CultureInfo.InvariantCulture, DateTimeStyles.None, out date))
{
// successfully parsed the string into a DateTime instance =>
// here we could add the desired number of months to it and construct
// a new DateTime
DateTime newDate = date.AddMonths(14);
}
else
{
// parsing failed => the specified string was not in the correct format
// you could inform the user about that here
}
You can DateTime.AddMonths to add months.
DateTime date = new DateTime(2013, 5, 19);
DateTime newDate = date.AddMonths(14);
In Java:
Calendar c = Calendar.getInstance();
c.setTime(new Date()); // today is the default
c.add(Calendar.DATE, 1); // number of days to add (1)
c.getTime(); // The new date
Just use AddMonths to add the specified number of months to the value of this instance.
DateTime date = new DateTime(2013, 5, 19); // (yyyy,MM,dd)
DateTime dt = date.AddMonths(14);