-1

Scenario:

My result = Report generation date - Visited date

Example:

Report generation date is : 20/07/2016

If Visited date is 19/07/2016 - result is 0, if Visited date is 18/07/2016 - result is 1, if Visited date is 17/07/2016 - result is 2, etc.

Racil Hilan
  • 24,690
  • 13
  • 50
  • 55
Sateesh
  • 15
  • 5
  • 5
    and what is stopping you? please, don't just copy paste your assignment here. at the very least, show us what you've tried, and what (didn't) work – Stultuske Jul 20 '16 at 10:45
  • See http://stackoverflow.com/questions/7103064/java-calculate-the-number-of-days-between-two-dates – Šimon Rozsíval Jul 20 '16 at 10:54

1 Answers1

0

Try This

 public static void main(String []args) {
    System.out.println(System.currentTimeMillis());
    System.out.println(new Date());
    Map<TimeUnit,Long> result = computeDiff(
            new Date("7/20/2016"),
        new Date("8/20/2016")
    );
    System.out.println(result);
 }

public static Map<TimeUnit,Long> computeDiff(Date date1, Date date2) {

    long diffInMillies = date2.getTime() - date1.getTime();
    List<TimeUnit> units = new ArrayList<TimeUnit>(EnumSet.allOf(TimeUnit.class));
    Collections.reverse(units);

    Map<TimeUnit,Long> result = new LinkedHashMap<TimeUnit,Long>();
    long milliesRest = diffInMillies;
    for ( TimeUnit unit : units ) {
        long diff = unit.convert(milliesRest,TimeUnit.MILLISECONDS);
        long diffInMilliesForUnit = unit.toMillis(diff);
        milliesRest = milliesRest - diffInMilliesForUnit;
        result.put(unit,diff);
    }
    return result;
}

}

  • Thanks for the code..... out put is look like..... {DAYS=1, HOURS=0, MINUTES=0, SECONDS=0, MILLISECONDS=0, MICROSECONDS=0, NANOSECONDS=0} . But i want only days value... like 1.. so pls suggest me/ give the code. – Sateesh Jul 20 '16 at 12:38
  • add break statment in for loop.so it will return only days value. – Murugan Deva Jul 21 '16 at 08:31
  • giving out put like {DAYS=7}... But i want only... 7.. how to get this? – Sateesh Jul 21 '16 at 11:35
  • public class Main { public static void main(String []args) { long diffInMillies = new Date("7/20/2016").getTime() - new Date("7/19/2016").getTime(); List units = new ArrayList(EnumSet.allOf(TimeUnit.class)); Collections.reverse(units); long milliesRest = diffInMillies; long diff = units.get(0).convert(milliesRest,TimeUnit.MILLISECONDS); System.out.println(diff); } } – Murugan Deva Jul 21 '16 at 12:12