Here; is my scenario;

A prisoner is admitted in a prison at this date:: 2011-05-05, he was sentenced for 5 years

so how do i calculate date additions so the results give me 2016-05-05, as this is the exact date he is to be released from prison.

the addition should be based on the number of years sentenced.
it looks like the algorithm should only add year, then months and days should be static.
help me out, thanx in advance.

question are included weekend and local hollydays???

basically you have (probably) two API

java.util.Calendar (allowed increase/decrease Date with integer value)
add one day -

cal.add(Calendar.DATE, +1);

and java.util.Date for difference between day (or whate .. with Date & Time)

private SimpleDateFormat sdf = new SimpleDateFormat("dd.MM.yyyy");
String removeTimeForDateString = sdf.format(######.getDate());
Date raiseTo = sdf.parse(removeTimeForDateString);
removeTimeForDateString = sdf.format(######.getDate());
Date raiseFrom = sdf.parse(removeTimeForDateString);
.
.
.
.
double noDays = (raiseTo.getTime() - raiseFrom.getTime());
noDays /= 1000;
noDays /= 60;
noDays /= 60;
noDays /= 24;
int decimalPlace = 0;
BigDecimal bd = new BigDecimal(noDays);
bd = bd.setScale(decimalPlace, BigDecimal.ROUND_HALF_UP);
noDays = bd.doubleValue();
long numberDays = (long) noDays;

note you have to RoundingUp, because there are missed one milisecond per one Day (in Long form) in java.util.Date

java.util.Calendar allows you to add days, or months, or years, just like you asked. But if you have to do this yourself, then yes, just add 5 to the year part and leave the month and day alone*

* unless, of course, the start date is 2012-02-29

@JamesCherrill

unless, of course, the start date is 2012-02-29

this was good one :-)

This is what i have done so far but it wont add yrs as desired:

class  FocusHandler implements FocusListener
      {
               
         java.util.Date today=new java.util.Date();
      	
      	
         DateFormat yrFormat = new SimpleDateFormat("yyyy");
         DateFormat monthFormat = new SimpleDateFormat("MM");
         DateFormat dayFormat = new SimpleDateFormat("dd");
      
         String yr =yrFormat.format( today);
         String month =monthFormat.format( today);
         String day =dayFormat.format( today);
      
      
      
          public void focusGained(FocusEvent e)
         {
            Object obj=(JTextField)e.getSource();
            if(obj==datetxt)
            {
               datetxt.setText(yr+ "-" + month+ "-" + day);
            
               String yrs = sentencetxt.getText();
               int Intyrs = Integer.parseInt(yrs);
            
               String rel  = yr+Intyrs;
               rDate.setText(rel);
             	
            }
         
         }
          public void focusLost(FocusEvent e)
         {
         }
      	
                    
      }

The result will something like 20115, instead it should be 2011 + 5 = 2016

String rel = yr+Intyrs;

yr is a String, so the + sign is treated as String concatenation, and Intyrs is converted to String and concatenated onto yr.
For this to work you need to + two ints.

and you can set Date instance in Model for JSpinner

Solved as follows and works perfectly:

public void focusGained(FocusEvent e)
         {
            Object obj=(JTextField)e.getSource();
            if(obj==datetxt)
            {
               try
               {
                  datetxt.setText(yr+ "-" + month+ "-" + day);
               
                  String yrs = sentencetxt.getText();
                  int Intyrs = Integer.parseInt(yrs);
               	
                  int yy = Integer.parseInt(yr);
               	
                  int rel  = yy+Intyrs;
                  rDate.setText(Integer.toString(rel) + "-" + month + "-" + day); 
               
               }
                   catch(Exception exp)
                  {
                     datetxt.setText("");
                  
                                      
                  }
            }

only needed to playy with conversions.

Excellent. Mark thi one solved now?

cal.add(Calendar.DATE, +1) eg:<<snip>>

commented: Spammer -3

i flagged this post as spam

commented: Good for you +13
commented: Thanks +16

i flagged this post as spam

Thank you for flagging. But next time, please don't post a reply to spam, that way I can just delete it and I won't have to leave half the spammer's post :)

Thanks again

thanks ... anyway nice avatar :-)

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.