Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / October 2006

Tip: Looking for answers? Try searching our database.

Adding months to Date

Thread view: 
Larry Coon - 08 Oct 2006 21:27 GMT
I understand the differnece between Date and Calendar, and why
I would prefer to use Calendar.  However, I'm stuck with Date
in this instance.

I want to add a secific number of months to a Date.  Looks like
I do it by converting to a Calendar, using Calendar.add(), and
converting back to Date.  The following SSCCE isn't working for
me -- endDate is unchanged from beginDate.  Can anyone point out
what I'm doing wrong (or if there's a better approach)?  Thanks.

import java.text.*;
import java.util.*;

public class Test {
 public static void main(String[] args) {
   DateFormat format = new SimpleDateFormat("mm/dd/yyyy");
   Date beginDate = format.parse("10/01/2005", new ParsePosition(0));

   GregorianCalendar calendar = new GregorianCalendar();
   calendar.setTime(beginDate);
   calendar.add(Calendar.MONTH, 3);
   Date endDate = calendar.getTime();

   System.out.println("Begin: " + format.format(beginDate));
   System.out.println("End: " + format.format(endDate));
 }
}
Filip Larsen - 08 Oct 2006 22:07 GMT
>     DateFormat format = new SimpleDateFormat("mm/dd/yyyy");

Use M instead of m.

Oh, and welcome to the club.

Regards,
Signature

Filip Larsen

Larry Coon - 08 Oct 2006 23:36 GMT
> Use M instead of m.
>
> Oh, and welcome to the club.

<embarrasment>
  *sigh* Thanks!
</embarrasment>


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.