-1

I am able to print the current date how to subtract 2 days from the current date using date object.Whatever the previous questions asked in this site are not matched with my requirement.If I am using calender then I am getting different format of date. I want date format in yyyyMMdd only by using date object.I tried this but I am getting error like the operator - is undefined for the argument type(s) String, int. help me how to resolve this.

Date date = new Date();
DateFormat dateFormat = new SimpleDateFormat("yyyyMMdd");
System.out.println("date is "+dateFormat.format(date));
Date dateBefore = new Date(dateFormat.format(date)) - (2 * 24 * 3600 * 1000));
usha
  • 11
  • 5
  • You can do it using `Calendar`, just calculate the difference as explained [here](https://stackoverflow.com/questions/11882926/how-to-subtract-x-day-from-a-date-object-in-java) and then call `dateFormat.format(calendar.getTime())` –  Oct 05 '17 at 17:32
  • [Your code does not generate that compilation error.](https://ideone.com/NjS9vq) – Sotirios Delimanolis Oct 05 '17 at 17:39
  • @Hugo ,I wrote the code like this Date date = new Date(); DateFormat dateFormat = new SimpleDateFormat("yyyyMMdd"); Calendar cal = Calendar.getInstance(); //cal.setTime(dateInstance); cal.add(Calendar.DATE, -2); Date dateBefore2Days = dateFormat.format(cal.getTime()); – usha Oct 05 '17 at 18:07
  • @Hugo, I am getting error like type mismatch can not convert from string to date – usha Oct 05 '17 at 18:08
  • `dateFormat.format()` returns a `String`, not a `Date`. You should do `dateBefore2Days = cal.getTime()`. Then, if you want to format the `Date` to a `String`, you call `dateFormat.format(dateBefore2Days)`. `Date` and `String` are different things. A `Date` [has no format](https://codeblog.jonskeet.uk/2017/04/23/all-about-java-util-date/), while a `String` returned by `format()` method **represents** the date in a specific format. –  Oct 05 '17 at 18:09
  • thanks for ur inputs ...its working fine now – usha Oct 05 '17 at 18:56

2 Answers2

0

You might want to look into Joda-time. It is much easier library to deal with dates in Java.

DateTime dateTime = new DateTime(date);
dateTime = dateTime.minusDays(2);

Here is how to format Joda-time: example

Edit: By definition, the dateFormat.format(date)) returns a string. Hence the compilation error that you get - can't deduct an int from a String. This is why I would suggest getting familiar with Joda-time

Community
  • 1
  • 1
ka_boom
  • 90
  • 2
  • 8
  • Although a great API (I used a lot, and really like it), Joda-Time is in maintainance mode and is being replaced by the new APIs, so I don't recommend start a new project with it. Even in [joda's website](http://www.joda.org/joda-time) it says: **"Note that Joda-Time is considered to be a largely “finished” project. No major enhancements are planned. If using Java SE 8, please migrate to java.time (JSR-310)."**. In Java 8, there's the [new java.time API](https://docs.oracle.com/javase/tutorial/datetime), and in JDK 6 and 7, the [ThreeTen Backport](http://www.threeten.org/threetenbp). –  Oct 05 '17 at 17:51
0

Use date.getTime() instead of dateFormat.format(date)

 Date dateBefore = new Date(date.getTime() - (2 * 24 * 3600 * 1000));
dspano
  • 1,540
  • 13
  • 25
  • @usha if this solves your problem, please mark this as the correct answer by clicking the check mark next to it. Thanks! – dspano Oct 05 '17 at 19:06