-9

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.

Community
  • 1
  • 1
Code Enthusiastic
  • 2,827
  • 5
  • 25
  • 39
  • 1
    is it in java, JS or C# ??? – Riju Mahna Jan 22 '13 at 09:06
  • @PradeepSimha I'm trying to avoid using built in methods offered by the libraries. – Code Enthusiastic Jan 22 '13 at 09:09
  • 2
    @CodeEnthusiastic You probably should have mentioned that in your question. Also, what's wrong with using the built in methods? – Simon Whitehead Jan 22 '13 at 09:09
  • Check also http://stackoverflow.com/questions/428918/how-can-i-increment-a-date-by-one-day-in-java – ioan Jan 22 '13 at 09:09
  • 2
    *"I'm trying to avoid using built in methods"* That is information that should be in the question, as should what you tried, in order to achieve that. – Andrew Thompson Jan 22 '13 at 09:10
  • 4
    @CodeEnthusiastic, reason for avoiding built-in methods? – Pradeep Simha Jan 22 '13 at 09:10
  • Here is an answer in Java - http://stackoverflow.com/questions/428918/how-can-i-increment-a-date-by-one-day-in-java – Dimitar Dimitrov Jan 22 '13 at 09:11
  • @PradeepSimha I am trying to avoid using built in methods in this case only. I am trying to improve the logical ability and validations. – Code Enthusiastic Jan 22 '13 at 09:12
  • 3
    You should not be trying to avoid the built-in methods. That's why they are built-in. In order to avoid you from reinventing the wheels. You should do that only if you want to shoot yourself in the feet. Is it what you want? You are not improving anything by avoiding them. You are shooting yourself or the people using and having to maintain your code. – Darin Dimitrov Jan 22 '13 at 09:12
  • 4
    @CodeEnthusiastic, these built in abstraction methods are provided so that you don't re-invent wheel and put more effort on business logic. _improve the logical ability and validations_ I don't think this is the best way to improve that – Pradeep Simha Jan 22 '13 at 09:15

4 Answers4

12

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
}
Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
2

You can DateTime.AddMonths to add months.

DateTime date = new DateTime(2013, 5, 19);
DateTime newDate = date.AddMonths(14);
Adil
  • 146,340
  • 25
  • 209
  • 204
0

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
ioan
  • 489
  • 2
  • 4
0

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);
Vishal Suthar
  • 17,013
  • 3
  • 59
  • 105