How to calculate a new date? - Using one date variable and a int variable

Ryan MacPhee :

Scenario

User can record their license using the application, they enter the day the license was redeemed and they also input the length of the license(days).

I am trying to figure out if you are able to calculate a new date from both these variables. for example

days = INPUT FROM USER;          days = 7;      
dateRedeemed = new Date();       date = 24/06/2019;      
newDate = dateRedeemed + days;   newDate = 01/07/2019;    
//Getting the values
            String name = txtName.getText();
            String contact = txtContact.getText();
            int years = Integer.parseInt(cboyears.getSelectedItem().toString());
            int months = Integer.parseInt(cboMonths.getSelectedItem().toString());
            int days = Integer.parseInt(cboDays.getSelectedItem().toString());


//Calculation            
            days = (years * 365) + (months * 12) + days;
            SimpleDateFormat format = new SimpleDateFormat("dd/MM/YYYY");
            Date dRedeemed = cboDate.getDate();
            String strRedeemed = format.format(dRedeemed);

If any one could help that would be great

Edit

Some great advice in this thread, Alot of you have been pointing out that the Date class in very bad and outdated i'm now going to start using the LocalDateTime Class as that seems more powerful, Another thing i would like to ask, is there a more efficient date picker. I have been using a swingx date picker is there a more effective choice?

Serj Ardovic :

Java (any) solution:

Date currentDate = new Date(); // or any date you set
Calendar c = Calendar.getInstance();
c.setTime(currentDate);
c.add(Calendar.DAY_OF_MONTH, 7);
Date currentDatePlusSevenDays = c.getTime();

Java 8+ solution:

Date currentDate = new Date(); // or any date you set
LocalDateTime localDateTime = currentDate.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime();
localDateTime = localDateTime.plusDays(7);
Date currentDatePlusSevenDays = Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant());

Java (any) solution with external library Joda-Time:

DateTime currentDate = DateTime.now(); // or any date you set
DateTime currentDatePlusSevenDays = currentDate.plusDays(7);   

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=146821&siteId=1