0

I am doing up a script where by the user will pass in string date (MM/DD/YYYY). I want to compare this string date to the "today" date to find out the days different between these 2 days.

This is my codes to get "today" date.

   DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
   Calendar cal = Calendar.getInstance();
   returnDate = dateFormat.format(cal.getTime());

How do i go about using date to minus date? convert them to timestamp and convert it back?

Thank you

Kimberlee Ho
  • 447
  • 1
  • 6
  • 23
  • 1
    http://stackoverflow.com/questions/1555262/calculating-the-difference-between-two-java-date-instances –  Oct 11 '13 at 11:14
  • Joda lib can help you, for more info check http://www.joda.org/joda-time/ – upog Oct 11 '13 at 11:14

3 Answers3

0

getTimeInMillis() and the relevant division will tell you how many days different. Not sure why converting it back is helpful.

Dan
  • 358
  • 1
  • 11
0

If you can use JodaTime library. A similar question with usage of this library was already answered here.

Community
  • 1
  • 1
0

In this first i get the current date and then minus by 2 then using formatter and set the same accordingly. So the ouput you will get will be 2 days before todays date, Please try below:-

Calendar cal=Calendar.getInstance();
        cal.add(Calendar.DAY_OF_MONTH, -2);
        Date dt=cal.getTime();
        SimpleDateFormat format = new SimpleDateFormat("dd/MM/yyyy");
        String ss=format.format(dt);
        System.out.println(ss);
Hussain Shabbir
  • 14,801
  • 5
  • 40
  • 56