Tan Chong Kai
Tan Chong Kai

Reputation: 332

How to add dates to the current date?

I want to add 90days into the current date and this is my code. I tried examples online but it doesn't seem to work.

    SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy");
    Calendar date = Calendar.getInstance();
    couponcreated = dateFormat.format(date);

    date.add(Calendar.DATE,90);
    SimpleDateFormat dateFormat1 = new SimpleDateFormat("dd-MM-yyyy");
    couponexpires = dateFormat1.format(date.getTime());

    couponexpiredate.setText(couponexpires);

However, I am getting errors and it causes my app to crash.

 java.lang.RuntimeException: Unable to start activity ComponentInfo{info.androidhive.loginandregistration/info.androidhive.loginandregistration.CouponReceivePageActivity}: java.lang.IllegalArgumentException: Bad class: class java.util.GregorianCalendar
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2295)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2349)
        at android.app.ActivityThread.access$700(ActivityThread.java:159)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1316)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:137)
        at android.app.ActivityThread.main(ActivityThread.java:5419)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:525)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1209)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1025)
        at dalvik.system.NativeStart.main(Native Method)
 Caused by: java.lang.IllegalArgumentException: Bad class: class java.util.GregorianCalendar
        at java.text.DateFormat.format(DateFormat.java:296)
        at java.text.Format.format(Format.java:93)
        at info.androidhive.loginandregistration.CouponReceivePageActivity.onCreate(CouponReceivePageActivity.java:55)
        at android.app.Activity.performCreate(Activity.java:5372)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1104)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2257)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2349)

        at android.app.ActivityThread.access$700(ActivityThread.java:159)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1316)  
        at android.os.Handler.dispatchMessage(Handler.java:99)    
        at android.os.Looper.loop(Looper.java:137)
        at android.app.ActivityThread.main(ActivityThread.java:5419)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:525)

This is my logcat code.

Upvotes: 1

Views: 137

Answers (1)

Niko Adrianus Yuwono
Niko Adrianus Yuwono

Reputation: 11122

Your mistake is when you first time trying to format the date
you should write :

couponcreated = dateFormat.format(date.getTime());

instead of

couponcreated = dateFormat.format(date);

because a Calendar can't be directly formatted, you need to get the Date from the Calendar using getTime() function

Upvotes: 1

Related Questions