-4

Hello guys i have a tricky question for you that i really cant find a solution out there.

What i want to do is to have 3 date/time inputs on simpledateformat

Date 1 
Date 2
Date 3

and basicaly i want to get difference of months days hours and minutes from date 1 - date2 and result of those 2 dates to be added on the firth date

for example 11/3/2017 12:30 - 7/3/2017 = 4 days and ADD that to current date 13/3/2017 13:30 + 4 days and 1 hour = 17/3/2017 14:30

i know how to get the diference in days hours and minutes , i cant get the second part of adding the result to the current date

any ideas?

thank you in dvance

Jens
  • 67,715
  • 15
  • 98
  • 113

4 Answers4

0

Get a date object using the simpledate format

Because it is an object of the same type Comparison is possible

0

Use Calendar class to add days and hours

Calendar cal = Calendar.getInstance();
cal.setTime(yourDate);
cal.add(Calendar.DAY_OF_MONTH, yourDays); //adds days to your date
cal.add(Calendar.HOUR_OF_DAY, yourHours); //adds hours to your date
cal.getTime(); //to get Date instance

To decrement dates just add negative number, for example:

int yourDays = -daysVariable;
int yourHours = -hoursVariable; //
Calendar cal = Calendar.getInstance();
cal.setTime(yourDate);
cal.add(Calendar.DAY_OF_MONTH, yourDays); //decrement days
cal.add(Calendar.HOUR_OF_DAY, yourHours); //decrement hours
cal.getTime(); //to get Date instance
miljon
  • 2,611
  • 1
  • 16
  • 19
  • I edited answer, just add negative number to your date do decrement date. If it is helpful please accept the answer and upvote ;) @Unlimited – miljon Mar 13 '17 at 10:01
  • 1
    i did but im very low on rep and my vote is recorded but not published :/ – Unlim ited Mar 13 '17 at 12:23
0

I think you need to use getTime and add days throught miliseconds.

For example, if you can get the difference of days you should use something like this

date1.getTime() + 24*60*60*1000*4

(where 4 is the difference you want to add)

You can use Calendar too.

0

Thank you all for your ultra fast replies!

I solved it by using Mij Solution

    Calendar cal = Calendar.getInstance();
    cal.setTime(yourDate);
    cal.add(Calendar.DAY_OF_MONTH, yourDays); //adds days to your date
    cal.add(Calendar.HOUR_OF_DAY, yourHours); //adds hours to your date
    cal.getTime(); //to get Date instance

To add days and

tis code to decrese days

    Calendar cal = Calendar.getInstance();
    cal.setTime(yourDate);
    cal.add(Calendar.DAY_OF_MONTH, -yourDays); //adds days to your date
    cal.add(Calendar.HOUR_OF_DAY, -yourHours); //adds hours to your date
    cal.getTime(); //to get Date instance

and to control if date is bigget than my current date i used this condition

     SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
     Date strDate = sdf.parse(valid_until);
     if (new Date().after(strDate)) {
     catalog_outdated = 1;
      }

it returns -1 if the date is past from current +1 if date is bigger that current or whatever and 0 if dates are equal

again thank you!