1

I want to add 30 days in my current date I searched a lot but did not get the proper solution.

my code

 Date date = new Date();
 SimpleDateFormat df = new SimpleDateFormat("YYYY-MM-dd");
 Calendar c1 = Calendar.getInstance();
 String currentdate = df.format(date);

 try {
     c1.setTime(df.parse(currentdate));
     c1.add(Calendar.DATE, 30);
     df = new SimpleDateFormat("yyyy-MM-dd");
     Date resultdate = new Date(c1.getTimeInMillis());
     String dueudate = df.format(resultdate);
     Toast.makeText(this, dueudate, Toast.LENGTH_SHORT).show();
 } catch (ParseException e) {
     e.printStackTrace();
 }

The output of this code is : 2019-01-29 I don't why it is showing this output can anyone help me.

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
A.J
  • 133
  • 1
  • 1
  • 11
  • Why do you create a new `Date` object AND a new `Calendar` object? `Calendar.getInstance()` sets the time to the current time, and if you REALLY need a `Date` object, you can call `Calendar#getTime()` after. – kenny_k Jun 04 '19 at 09:12
  • Possible duplicate question https://stackoverflow.com/questions/3747490/android-get-date-before-7-days-one-week – Mohsin kazi Jun 04 '19 at 09:36
  • Search Stack Overflow before posting. – Basil Bourque Jun 05 '19 at 19:39

4 Answers4

5

You need to use

c1.add(Calendar.DAY_OF_YEAR, 30);

instead of

c1.add(Calendar.DATE, 30);

Try this

    Date date = new Date();
    SimpleDateFormat df  = new SimpleDateFormat("YYYY-MM-dd");
    Calendar c1 = Calendar.getInstance();
    String currentDate = df.format(date);// get current date here

    // now add 30 day in Calendar instance 
    c1.add(Calendar.DAY_OF_YEAR, 30);
    df = new SimpleDateFormat("yyyy-MM-dd");
    Date resultDate = c1.getTime();
    String     dueDate = df.format(resultDate);

    // print the result
    Utils.printLog("DATE_DATE :-> "+currentDate);
    Utils.printLog("DUE_DATE :-> "+dueDate);

OUTPUT

2019-06-04 14:43:02.438 E/XXX_XXXX: DATE_DATE :-> 2019-06-04
2019-06-04 14:43:02.438 E/XXX_XXXX: DUE_DATE :-> 2019-07-04
AskNilesh
  • 67,701
  • 16
  • 123
  • 163
  • 1
    Wrong. Whether you use `DAY_OF_YEAR` or `DATE` when adding days makes no difference. (That’s just a minor one of the confusing points about `Calendar`.) – Ole V.V. Jun 04 '19 at 13:19
  • 1
    Also, while your code gives the expected result today, your first format pattern string is wrong and will give you surprises around New Year. – Ole V.V. Jun 04 '19 at 13:25
  • FYI, the troublesome date-time classes such as `java.util.Date`, `java.util.Calendar`, and `java.text.SimpleDateFormat` are now legacy, supplanted by the [*java.time*](https://docs.oracle.com/javase/10/docs/api/java/time/package-summary.html) classes. Most of the *java.time* functionality is back-ported to Java 6 & Java 7 in the [***ThreeTen-Backport***](http://www.threeten.org/threetenbp/) project. Further adapted for earlier Android (<26) in [***ThreeTenABP***](https://github.com/JakeWharton/ThreeTenABP). See [*How to use ThreeTenABP…*](http://stackoverflow.com/q/38922754/642706). – Basil Bourque Jun 05 '19 at 19:37
2

Another easier option, if on Java 8, use the java.time package which provides functions to perform plus/minus on current date of any units of time, example:

import java.time.LocalDate;


 LocalDate date =  LocalDate.now().plusDays(30);

 //or
 LocalDate date =  LocalDate.now().plus(30, ChronoUnit.DAYS);
TechFree
  • 2,600
  • 1
  • 17
  • 18
  • 1
    This is the good answer, thanks. To use `LocalDate` on older Android (API level under 26), add [ThreeTenABP](https://github.com/JakeWharton/ThreeTenABP) to your Android project and make sure to import `org.threeten.bp.LocalDate`. I recommend giving time zone to `now()`, for example `LocalDate.now(ZoneId.of("America/Resolute"))`. – Ole V.V. Jun 04 '19 at 13:23
  • 1
    Tip: Better to explicitly specify the optional `ZoneId` argument, even if you use `ZoneId.systemDefault`, so the reader knows you considered the crucial issue of time zone. – Basil Bourque Jun 04 '19 at 14:00
1

Calendar.getInstance() gives you the current time. You don't need to create another Date object for that.

    Calendar current = Calendar.getInstance();
    current.add(Calendar.DATE, 30);

    SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");

    Date resultdate = new Date(current.getTimeInMillis());
    String dueudate = df.format(resultdate);

    System.out.println("" + dueudate);
Gunhan
  • 6,807
  • 3
  • 43
  • 37
0

1)Go from date to millis. 2)Create a long variable with value 30L * 24L * 60L * 60L * 1000L. 3)Add this value to the millis you got in step 1 4) Go from this sum back to date again.

Edit: Variables that store millis should be long, not int. Edit2: Adding "L" besides each number guarantees we won't get an overflow.

  • Storing into a `long`variable is not enough. Your multiplication overflows and results in -1 702 967 296. Storing that into a `long` does not remedy the situation. In any case doing your own time math is never recommended exactly because it is so error-prone. Use well-proven library methods instead. – Ole V.V. Jun 04 '19 at 14:25