How to add an integer no to a date so tat i can a new date 
foe ex: date=10/11/2012
        no=5
        newdate=15/10/2012
        **Any body plz help to solve the problem**

Recommended Answers

All 5 Replies

First off, are you using a system Date (java.util.Date) or a database Date (java.sql.Date)? I am assuming the former, but if you are working with a SQL database, you may be talking about the latter.

Either way, you will want to use one of the Calendar classes, probably GregorianCalendar. The Calendar classes all support a method called .add() which will do the job you want.

However, when adding or subtracting time from a Calendar object, you need to tell it what incement of time to add - an hour, a day, a year, a month, whichever. so what you would have is something along these lines:

Calendar cal = new GregorianCalendar();   // default is current time and default zone
cal.add(Calendar.DATE, 5);

Note that I haven't addressed the question of time zone, which you may neecd to do.

I am using java.util.date and i am taking input from textfield. if i give date as 30/10/2012 and add 2 to will it show 2/11/2012. please explain me in details how to add an integer no to a date

Schol-R-LEA gave you the answer. GregorianCalendar. Look it up in the API doc.

 private void txtStartKeyReleased(java.awt.event.KeyEvent evt) {
       try {    

        Date date1;
        date1 = new SimpleDateFormat("yyyy-MM-dd").parse(txtStart.getText());
        System.out.println(date1);

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");     
        Calendar cal  = Calendar.getInstance();
                      cal.setTime(date1);
                        cal.add(Calendar.DATE, 102);
                        String expDateString = sdf.format(cal.getTime());
                        txtExpiry.setText(expDateString);
     }catch (ParseException ex) {
      Logger.getLogger(ClientInfo.class.getName()).log(Level.SEVERE, null, ex);
     } 
}  

try this....and reply me..

commented: Teaches nothing, just invites copy/paste cheating -3
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.