4
Contributors
5
Replies
8
Views
4 Years
Discussion Span
Last Post by anandschiru
Featured 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`](http://docs.oracle.com/javase/7/docs/api/java/util/GregorianCalendar.html). The … Read More

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

2

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.

0

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

Edited by anandschiru: m

-1
 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..

Comments
Teaches nothing, just invites copy/paste cheating
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.